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
);
309 int get_linux_version()
313 int a
= 0, b
= 0,c
= 0;
318 a
= strtoul(cp
, &cp
, 10);
320 b
= strtoul(cp
+1, &cp
, 10);
322 c
= strtoul(cp
+1, &cp
, 10);
324 return (a
*1000000)+(b
*1000)+c
;
327 int mdadm_version(char *version
)
335 cp
= strchr(version
, '-');
336 if (!cp
|| *(cp
+1) != ' ' || *(cp
+2) != 'v')
339 a
= strtoul(cp
, &cp
, 10);
342 b
= strtoul(cp
+1, &cp
, 10);
344 c
= strtoul(cp
+1, &cp
, 10);
347 if (*cp
!= ' ' && *cp
!= '-')
349 return (a
*1000000)+(b
*1000)+c
;
352 unsigned long long parse_size(char *size
)
354 /* parse 'size' which should be a number optionally
355 * followed by 'K', 'M'. 'G' or 'T'.
356 * Without a suffix, K is assumed.
357 * Number returned is in sectors (half-K)
358 * INVALID_SECTORS returned on error.
361 long long s
= strtoll(size
, &c
, 10);
375 s
*= 1024 * 1024 * 2;
379 s
*= 1024 * 1024 * 1024 * 2LL;
381 case 's': /* sectors */
392 int is_near_layout_10(int layout
)
396 fc
= (layout
>> 8) & 255;
397 fo
= layout
& (1 << 16);
398 if (fc
> 1 || fo
> 0)
403 int parse_layout_10(char *layout
)
407 /* Parse the layout string for raid10 */
408 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
409 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
410 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
414 if (layout
[0] == 'n')
416 else if (layout
[0] == 'o')
417 rv
= 0x10000 + (copies
<<8) + 1;
419 rv
= 1 + (copies
<<8);
423 int parse_layout_faulty(char *layout
)
427 /* Parse the layout string for 'faulty' */
428 int ln
= strcspn(layout
, "0123456789");
429 char *m
= xstrdup(layout
);
432 mode
= map_name(faultylayout
, m
);
436 return mode
| (atoi(layout
+ln
)<< ModeShift
);
439 int parse_cluster_confirm_arg(char *input
, char **devname
, int *slot
)
442 *slot
= strtoul(input
, &dev
, 10);
443 if (dev
== input
|| dev
[0] != ':')
449 void remove_partitions(int fd
)
451 /* remove partitions from this block devices.
452 * This is used for components added to an array
454 #ifdef BLKPG_DEL_PARTITION
455 struct blkpg_ioctl_arg a
;
456 struct blkpg_partition p
;
458 a
.op
= BLKPG_DEL_PARTITION
;
460 a
.datalen
= sizeof(p
);
462 memset(a
.data
, 0, a
.datalen
);
463 for (p
.pno
= 0; p
.pno
< 16; p
.pno
++)
464 ioctl(fd
, BLKPG
, &a
);
468 int test_partition(int fd
)
470 /* Check if fd is a whole-disk or a partition.
471 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
472 * will return ENXIO on an invalid partition number.
474 struct blkpg_ioctl_arg a
;
475 struct blkpg_partition p
;
476 a
.op
= BLKPG_DEL_PARTITION
;
478 a
.datalen
= sizeof(p
);
480 memset(a
.data
, 0, a
.datalen
);
482 if (ioctl(fd
, BLKPG
, &a
) == 0)
483 /* Very unlikely, but not a partition */
485 if (errno
== ENXIO
|| errno
== ENOTTY
)
486 /* not a partition */
492 int test_partition_from_id(dev_t id
)
497 sprintf(buf
, "%d:%d", major(id
), minor(id
));
498 fd
= dev_open(buf
, O_RDONLY
);
501 rv
= test_partition(fd
);
506 int enough(int level
, int raid_disks
, int layout
, int clean
, char *avail
)
512 for (i
= 0; i
< raid_disks
; i
++)
513 avail_disks
+= !!avail
[i
];
517 /* This is the tricky one - we need to check
518 * which actual disks are present.
520 copies
= (layout
&255)* ((layout
>>8) & 255);
523 /* there must be one of the 'copies' form 'first' */
530 this = (this+1) % raid_disks
;
534 first
= (first
+(layout
&255)) % raid_disks
;
535 } while (first
!= 0);
538 case LEVEL_MULTIPATH
:
539 return avail_disks
>= 1;
542 return avail_disks
== raid_disks
;
544 return avail_disks
>= 1;
546 if (avail_disks
== raid_disks
- 1 &&
547 !avail
[raid_disks
- 1])
548 /* If just the parity device is missing, then we
549 * have enough, even if not clean
555 return avail_disks
>= raid_disks
-1;
557 return avail_disks
>= raid_disks
;
560 return avail_disks
>= raid_disks
-2;
562 return avail_disks
>= raid_disks
;
568 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
575 copy_uuid(uuid
, id
, swap
);
576 for (i
= 0; i
< 4; i
++) {
579 for (j
= 3; j
>= 0; j
--) {
580 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
588 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
,
591 // dirty hack to work around an issue with super1 superblocks...
592 // super1 superblocks need swapuuid set in order for assembly to
593 // work, but can't have it set if we want this printout to match
594 // all the other uuid printouts in super1.c, so we force swapuuid
595 // to 1 to make our printout match the rest of super1
596 #if __BYTE_ORDER == BIG_ENDIAN
597 return __fname_from_uuid(info
->uuid
, 1, buf
, sep
);
599 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 :
600 st
->ss
->swapuuid
, buf
, sep
);
604 int check_ext2(int fd
, char *name
)
607 * Check for an ext2fs file system.
608 * Superblock is always 1K at 1K offset
610 * s_magic is le16 at 56 == 0xEF53
611 * report mtime - le32 at 44
613 * logblksize - le32 at 24
615 unsigned char sb
[1024];
617 unsigned long long size
;
619 if (lseek(fd
, 1024,0)!= 1024)
621 if (read(fd
, sb
, 1024)!= 1024)
623 if (sb
[56] != 0x53 || sb
[57] != 0xef)
626 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
627 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
628 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
630 pr_err("%s appears to contain an ext2fs file system\n",
632 cont_err("size=%lluK mtime=%s", size
, ctime(&mtime
));
636 int check_reiser(int fd
, char *name
)
639 * superblock is at 64K
641 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
644 unsigned char sb
[1024];
645 unsigned long long size
;
646 if (lseek(fd
, 64*1024, 0) != 64*1024)
648 if (read(fd
, sb
, 1024) != 1024)
650 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
651 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
653 pr_err("%s appears to contain a reiserfs file system\n",name
);
654 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
655 cont_err("size = %lluK\n", size
*4);
660 int check_raid(int fd
, char *name
)
665 struct supertype
*st
= guess_super(fd
);
669 if (st
->ss
->add_to_super
!= NULL
) {
670 st
->ss
->load_super(st
, fd
, name
);
671 /* Looks like a raid array .. */
672 pr_err("%s appears to be part of a raid array:\n", name
);
673 st
->ss
->getinfo_super(st
, &info
, NULL
);
674 st
->ss
->free_super(st
);
675 crtime
= info
.array
.ctime
;
676 level
= map_num(pers
, info
.array
.level
);
679 cont_err("level=%s devices=%d ctime=%s",
680 level
, info
.array
.raid_disks
, ctime(&crtime
));
682 /* Looks like GPT or MBR */
683 pr_err("partition table exists on %s\n", name
);
688 int fstat_is_blkdev(int fd
, char *devname
, dev_t
*rdev
)
692 if (fstat(fd
, &stb
) != 0) {
693 pr_err("fstat failed for %s: %s\n", devname
, strerror(errno
));
696 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
) {
697 pr_err("%s is not a block device.\n", devname
);
705 int stat_is_blkdev(char *devname
, dev_t
*rdev
)
709 if (stat(devname
, &stb
) != 0) {
710 pr_err("stat failed for %s: %s\n", devname
, strerror(errno
));
713 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
) {
714 pr_err("%s is not a block device.\n", devname
);
726 for (i
= 0; i
< 5; i
++) {
728 fprintf(stderr
, "%s%s", mesg
, add
);
730 if (fgets(buf
, 100, stdin
)==NULL
)
732 if (buf
[0]=='y' || buf
[0]=='Y')
734 if (buf
[0]=='n' || buf
[0]=='N')
738 pr_err("assuming 'no'\n");
742 int is_standard(char *dev
, int *nump
)
744 /* tests if dev is a "standard" md dev name.
745 * i.e if the last component is "/dNN" or "/mdNN",
746 * where NN is a string of digits
747 * Returns 1 if a partitionable standard,
748 * -1 if non-partitonable,
749 * 0 if not a standard name.
751 char *d
= strrchr(dev
, '/');
756 if (strncmp(d
, "/d",2) == 0)
757 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
758 else if (strncmp(d
, "/md_d", 5) == 0)
759 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
760 else if (strncmp(d
, "/md", 3) == 0)
761 d
+= 3, type
= -1; /* /dev/mdN */
762 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
763 d
+= 1, type
= -1; /* /dev/md/N */
773 if (nump
) *nump
= num
;
778 unsigned long calc_csum(void *super
, int bytes
)
780 unsigned long long newcsum
= 0;
783 unsigned int *superc
= (unsigned int*) super
;
785 for(i
= 0; i
< bytes
/4; i
++)
786 newcsum
+= superc
[i
];
787 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
789 /* The in-kernel checksum calculation is always 16bit on
790 * the alpha, though it is 32 bit on i386...
791 * I wonder what it is elsewhere... (it uses an API in
792 * a way that it shouldn't).
794 csum
= (csum
& 0xffff) + (csum
>> 16);
795 csum
= (csum
& 0xffff) + (csum
>> 16);
800 char *human_size(long long bytes
)
804 /* We convert bytes to either centi-M{ega,ibi}bytes,
805 * centi-G{igi,ibi}bytes or centi-T{era,ebi}bytes
806 * with appropriate rounding, and then print
807 * 1/100th of those as a decimal.
808 * We allow upto 2048Megabytes before converting to
809 * gigabytes and 2048Gigabytes before converting to
810 * terabytes, as that shows more precision and isn't
811 * too large a number.
814 if (bytes
< 5000*1024)
816 else if (bytes
< 2*1024LL*1024LL*1024LL) {
817 long cMiB
= (bytes
* 200LL / (1LL<<20) + 1) / 2;
818 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
819 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
820 cMiB
/100, cMiB
% 100, cMB
/100, cMB
% 100);
821 } else if (bytes
< 2*1024LL*1024LL*1024LL*1024LL) {
822 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) / 2;
823 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
824 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
825 cGiB
/100, cGiB
% 100, cGB
/100, cGB
% 100);
827 long cTiB
= (bytes
* 200LL / (1LL<<40) + 1) / 2;
828 long cTB
= (bytes
/ (1000000000000LL / 200LL) + 1) / 2;
829 snprintf(buf
, sizeof(buf
), " (%ld.%02ld TiB %ld.%02ld TB)",
830 cTiB
/100, cTiB
% 100, cTB
/100, cTB
% 100);
835 char *human_size_brief(long long bytes
, int prefix
)
839 /* We convert bytes to either centi-M{ega,ibi}bytes,
840 * centi-G{igi,ibi}bytes or centi-T{era,ebi}bytes
841 * with appropriate rounding, and then print
842 * 1/100th of those as a decimal.
843 * We allow upto 2048Megabytes before converting to
844 * gigabytes and 2048Gigabytes before converting to
845 * terabytes, as that shows more precision and isn't
846 * too large a number.
848 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
849 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
852 if (bytes
< 5000*1024)
854 else if (prefix
== IEC
) {
855 if (bytes
< 2*1024LL*1024LL*1024LL) {
856 long cMiB
= (bytes
* 200LL / (1LL<<20) +1) /2;
857 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
858 cMiB
/100, cMiB
% 100);
859 } else if (bytes
< 2*1024LL*1024LL*1024LL*1024LL) {
860 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) /2;
861 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
862 cGiB
/100, cGiB
% 100);
864 long cTiB
= (bytes
* 200LL / (1LL<<40) + 1) / 2;
865 snprintf(buf
, sizeof(buf
), "%ld.%02ldTiB",
866 cTiB
/100, cTiB
% 100);
869 else if (prefix
== JEDEC
) {
870 if (bytes
< 2*1024LL*1024LL*1024LL) {
871 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
872 snprintf(buf
, sizeof(buf
), "%ld.%02ldMB",
874 } else if (bytes
< 2*1024LL*1024LL*1024LL*1024LL) {
875 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
876 snprintf(buf
, sizeof(buf
), "%ld.%02ldGB",
879 long cTB
= (bytes
/ (1000000000000LL / 200LL) + 1) / 2;
880 snprintf(buf
, sizeof(buf
), "%ld.%02ldTB",
890 void print_r10_layout(int layout
)
892 int near
= layout
& 255;
893 int far
= (layout
>> 8) & 255;
894 int offset
= (layout
&0x10000);
898 printf("%s near=%d", sep
, near
);
902 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
904 printf("NO REDUNDANCY");
907 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
908 int chunksize
, unsigned long long devsize
)
912 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
913 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
916 int get_data_disks(int level
, int layout
, int raid_disks
)
920 case 0: data_disks
= raid_disks
;
922 case 1: data_disks
= 1;
925 case 5: data_disks
= raid_disks
- 1;
927 case 6: data_disks
= raid_disks
- 2;
929 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
936 dev_t
devnm2devid(char *devnm
)
938 /* First look in /sys/block/$DEVNM/dev for %d:%d
939 * If that fails, try parsing out a number
946 snprintf(path
, sizeof(path
), "/sys/block/%s/dev", devnm
);
947 fd
= open(path
, O_RDONLY
);
950 int n
= read(fd
, buf
, sizeof(buf
));
954 if (n
> 0 && sscanf(buf
, "%d:%d\n", &mjr
, &mnr
) == 2)
955 return makedev(mjr
, mnr
);
957 if (strncmp(devnm
, "md_d", 4) == 0 &&
959 (mnr
= strtoul(devnm
+4, &ep
, 10)) >= 0 &&
960 ep
> devnm
&& *ep
== 0)
961 return makedev(get_mdp_major(), mnr
<< MdpMinorShift
);
963 if (strncmp(devnm
, "md", 2) == 0 &&
965 (mnr
= strtoul(devnm
+2, &ep
, 10)) >= 0 &&
966 ep
> devnm
&& *ep
== 0)
967 return makedev(MD_MAJOR
, mnr
);
972 char *get_md_name(char *devnm
)
974 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
975 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
977 static char devname
[50];
979 dev_t rdev
= devnm2devid(devnm
);
984 if (strncmp(devnm
, "md_", 3) == 0) {
985 snprintf(devname
, sizeof(devname
), "/dev/md/%s",
987 if (stat(devname
, &stb
) == 0 &&
988 (S_IFMT
&stb
.st_mode
) == S_IFBLK
&& (stb
.st_rdev
== rdev
))
991 snprintf(devname
, sizeof(devname
), "/dev/%s", devnm
);
992 if (stat(devname
, &stb
) == 0 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
&&
993 (stb
.st_rdev
== rdev
))
996 snprintf(devname
, sizeof(devname
), "/dev/md/%s", devnm
+2);
997 if (stat(devname
, &stb
) == 0 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
&&
998 (stb
.st_rdev
== rdev
))
1001 dn
= map_dev(major(rdev
), minor(rdev
), 0);
1004 snprintf(devname
, sizeof(devname
), "/dev/.tmp.%s", devnm
);
1005 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
1006 if (errno
!= EEXIST
)
1009 if (stat(devname
, &stb
) == 0 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
&&
1010 (stb
.st_rdev
== rdev
))
1016 void put_md_name(char *name
)
1018 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
1022 int get_maj_min(char *dev
, int *major
, int *minor
)
1025 *major
= strtoul(dev
, &e
, 0);
1026 return (e
> dev
&& *e
== ':' && e
[1] &&
1027 (*minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
1031 int dev_open(char *dev
, int flags
)
1033 /* like 'open', but if 'dev' matches %d:%d, create a temp
1034 * block device and open that
1045 if (get_maj_min(dev
, &major
, &minor
)) {
1046 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
1047 (int)getpid(), major
, minor
);
1048 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
1049 fd
= open(devname
, flags
);
1053 /* Try /tmp as /dev appear to be read-only */
1054 snprintf(devname
, sizeof(devname
),
1055 "/tmp/.tmp.md.%d:%d:%d",
1056 (int)getpid(), major
, minor
);
1057 if (mknod(devname
, S_IFBLK
|0600,
1058 makedev(major
, minor
)) == 0) {
1059 fd
= open(devname
, flags
);
1064 fd
= open(dev
, flags
);
1068 int open_dev_flags(char *devnm
, int flags
)
1073 devid
= devnm2devid(devnm
);
1074 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
1075 return dev_open(buf
, flags
);
1078 int open_dev(char *devnm
)
1080 return open_dev_flags(devnm
, O_RDONLY
);
1083 int open_dev_excl(char *devnm
)
1088 dev_t devid
= devnm2devid(devnm
);
1091 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
1092 for (i
= 0; i
< 25; i
++) {
1093 int fd
= dev_open(buf
, flags
|O_EXCL
);
1096 if (errno
== EACCES
&& flags
== O_RDWR
) {
1109 int same_dev(char *one
, char *two
)
1111 struct stat st1
, st2
;
1112 if (stat(one
, &st1
) != 0)
1114 if (stat(two
, &st2
) != 0)
1116 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
1118 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
1120 return st1
.st_rdev
== st2
.st_rdev
;
1123 void wait_for(char *dev
, int fd
)
1126 struct stat stb_want
;
1129 if (fstat(fd
, &stb_want
) != 0 ||
1130 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
1133 for (i
= 0; i
< 25; i
++) {
1135 if (stat(dev
, &stb
) == 0 &&
1136 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1137 (stb
.st_rdev
== stb_want
.st_rdev
))
1144 pr_err("timeout waiting for %s\n", dev
);
1147 struct superswitch
*superlist
[] =
1150 &super_ddf
, &super_imsm
,
1155 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
1157 mdu_array_info_t array
;
1160 struct supertype
*st
= NULL
;
1165 char *subarray
= NULL
;
1166 char container
[32] = "";
1168 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
1171 vers
= sra
->array
.major_version
;
1172 minor
= sra
->array
.minor_version
;
1173 verstr
= sra
->text_version
;
1175 if (md_get_array_info(fd
, &array
))
1176 array
.major_version
= array
.minor_version
= 0;
1177 vers
= array
.major_version
;
1178 minor
= array
.minor_version
;
1183 sprintf(version
, "%d.%d", vers
, minor
);
1186 if (minor
== -2 && is_subarray(verstr
)) {
1187 char *dev
= verstr
+1;
1189 subarray
= strchr(dev
, '/');
1192 subarray
= xstrdup(subarray
);
1194 strcpy(container
, dev
);
1196 sra
= sysfs_read(-1, container
, GET_VERSION
);
1197 if (sra
&& sra
->text_version
[0])
1198 verstr
= sra
->text_version
;
1200 verstr
= "-no-metadata-";
1203 for (i
= 0; st
== NULL
&& superlist
[i
]; i
++)
1204 st
= superlist
[i
]->match_metadata_desc(verstr
);
1210 *subarrayp
= subarray
;
1211 strcpy(st
->container_devnm
, container
);
1212 strcpy(st
->devnm
, fd2devnm(fd
));
1219 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1224 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1225 fd
= dev_open(buf
, O_RDONLY
);
1228 if (get_dev_size(fd
, NULL
, size
)) {
1236 int dev_sector_size_from_id(dev_t id
, unsigned int *size
)
1241 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1242 fd
= dev_open(buf
, O_RDONLY
);
1245 if (get_dev_sector_size(fd
, NULL
, size
)) {
1253 struct supertype
*dup_super(struct supertype
*orig
)
1255 struct supertype
*st
;
1259 st
= xcalloc(1, sizeof(*st
));
1261 st
->max_devs
= orig
->max_devs
;
1262 st
->minor_version
= orig
->minor_version
;
1263 st
->ignore_hw_compat
= orig
->ignore_hw_compat
;
1264 st
->data_offset
= orig
->data_offset
;
1270 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1272 /* try each load_super to find the best match,
1273 * and return the best superswitch
1275 struct superswitch
*ss
;
1276 struct supertype
*st
;
1277 unsigned int besttime
= 0;
1281 st
= xcalloc(1, sizeof(*st
));
1282 st
->container_devnm
[0] = 0;
1284 for (i
= 0; superlist
[i
]; i
++) {
1287 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1289 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1291 memset(st
, 0, sizeof(*st
));
1292 st
->ignore_hw_compat
= 1;
1293 rv
= ss
->load_super(st
, fd
, NULL
);
1296 st
->ss
->getinfo_super(st
, &info
, NULL
);
1297 if (bestsuper
== -1 ||
1298 besttime
< info
.array
.ctime
) {
1300 besttime
= info
.array
.ctime
;
1305 if (bestsuper
!= -1) {
1307 memset(st
, 0, sizeof(*st
));
1308 st
->ignore_hw_compat
= 1;
1309 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1311 superlist
[bestsuper
]->free_super(st
);
1319 /* Return size of device in bytes */
1320 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1322 unsigned long long ldsize
;
1325 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1326 ldsize
= (unsigned long long)st
.st_size
;
1329 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1332 unsigned long dsize
;
1333 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1338 pr_err("Cannot get size of %s: %s\n",
1339 dname
, strerror(errno
));
1347 /* Return sector size of device in bytes */
1348 int get_dev_sector_size(int fd
, char *dname
, unsigned int *sectsizep
)
1350 unsigned int sectsize
;
1352 if (ioctl(fd
, BLKSSZGET
, §size
) != 0) {
1354 pr_err("Cannot get sector size of %s: %s\n",
1355 dname
, strerror(errno
));
1359 *sectsizep
= sectsize
;
1363 /* Return true if this can only be a container, not a member device.
1364 * i.e. is and md device and size is zero
1366 int must_be_container(int fd
)
1369 unsigned long long size
;
1371 mdi
= sysfs_read(fd
, NULL
, GET_VERSION
);
1376 if (get_dev_size(fd
, NULL
, &size
) == 0)
1383 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1384 * Returns: 1 if successful
1385 * -1 for unknown partition type
1386 * 0 for other errors
1388 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1391 unsigned char empty_gpt_entry
[16]= {0};
1392 struct GPT_part_entry
*part
;
1394 unsigned long long curr_part_end
;
1395 unsigned all_partitions
, entry_size
;
1397 unsigned int sector_size
= 0;
1401 BUILD_BUG_ON(sizeof(gpt
) != 512);
1402 /* skip protective MBR */
1403 if (!get_dev_sector_size(fd
, NULL
, §or_size
))
1405 lseek(fd
, sector_size
, SEEK_SET
);
1406 /* read GPT header */
1407 if (read(fd
, &gpt
, 512) != 512)
1410 /* get the number of partition entries and the entry size */
1411 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1412 entry_size
= __le32_to_cpu(gpt
.part_size
);
1414 /* Check GPT signature*/
1415 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1419 if (all_partitions
> 1024 ||
1420 entry_size
> sizeof(buf
))
1423 part
= (struct GPT_part_entry
*)buf
;
1425 /* set offset to third block (GPT entries) */
1426 lseek(fd
, sector_size
*2, SEEK_SET
);
1427 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1428 /* read partition entry */
1429 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1432 /* is this valid partition? */
1433 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1434 /* check the last lba for the current partition */
1435 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1436 if (curr_part_end
> *endofpart
)
1437 *endofpart
= curr_part_end
;
1444 /* Sets endofpart parameter to the last block used by the last partition on the device.
1445 * Returns: 1 if successful
1446 * -1 for unknown partition type
1447 * 0 for other errors
1449 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1451 struct MBR boot_sect
;
1452 unsigned long long curr_part_end
;
1454 unsigned int sector_size
;
1459 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1462 if (read(fd
, &boot_sect
, 512) != 512)
1465 /* check MBP signature */
1466 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1468 /* found the correct signature */
1470 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1472 * Have to make every access through boot_sect rather
1473 * than using a pointer to the partition table (or an
1474 * entry), since the entries are not properly aligned.
1477 /* check for GPT type */
1478 if (boot_sect
.parts
[part_nr
].part_type
==
1479 MBR_GPT_PARTITION_TYPE
) {
1480 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1483 /* check the last used lba for the current partition */
1485 __le32_to_cpu(boot_sect
.parts
[part_nr
].first_sect_lba
) +
1486 __le32_to_cpu(boot_sect
.parts
[part_nr
].blocks_num
);
1487 if (curr_part_end
> *endofpart
)
1488 *endofpart
= curr_part_end
;
1491 /* Unknown partition table */
1494 /* calculate number of 512-byte blocks */
1495 if (get_dev_sector_size(fd
, NULL
, §or_size
))
1496 *endofpart
*= (sector_size
/ 512);
1501 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1502 unsigned long long size
)
1505 * Check where the last partition ends
1507 unsigned long long endofpart
;
1509 if (get_last_partition_end(fd
, &endofpart
) > 0) {
1510 /* There appears to be a partition table here */
1511 if (freesize
== 0) {
1512 /* partitions will not be visible in new device */
1513 pr_err("partition table exists on %s but will be lost or\n"
1514 " meaningless after creating array\n",
1517 } else if (endofpart
> freesize
) {
1518 /* last partition overlaps metadata */
1519 pr_err("metadata will over-write last partition on %s.\n",
1522 } else if (size
&& endofpart
> size
) {
1523 /* partitions will be truncated in new device */
1524 pr_err("array size is too small to cover all partitions on %s.\n",
1532 int open_container(int fd
)
1534 /* 'fd' is a block device. Find out if it is in use
1535 * by a container, and return an open fd on that container.
1546 if (fstat(fd
, &st
) != 0)
1548 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1549 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1550 e
= path
+ strlen(path
);
1552 dir
= opendir(path
);
1555 while ((de
= readdir(dir
))) {
1558 if (de
->d_name
[0] == '.')
1560 /* Need to make sure it is a container and not a volume */
1561 sprintf(e
, "/%s/md/metadata_version", de
->d_name
);
1562 dfd
= open(path
, O_RDONLY
);
1565 n
= read(dfd
, buf
, sizeof(buf
));
1567 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1570 if (strncmp(buf
, "external", 8) != 0 ||
1574 sprintf(e
, "/%s/dev", de
->d_name
);
1575 dfd
= open(path
, O_RDONLY
);
1578 n
= read(dfd
, buf
, sizeof(buf
));
1580 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1583 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1585 sprintf(buf
, "%d:%d", major
, minor
);
1586 dfd
= dev_open(buf
, O_RDONLY
);
1596 struct superswitch
*version_to_superswitch(char *vers
)
1600 for (i
= 0; superlist
[i
]; i
++) {
1601 struct superswitch
*ss
= superlist
[i
];
1603 if (strcmp(vers
, ss
->name
) == 0)
1610 int metadata_container_matches(char *metadata
, char *devnm
)
1612 /* Check if 'devnm' is the container named in 'metadata'
1614 * /containername/componentname or
1615 * -containername/componentname
1618 if (*metadata
!= '/' && *metadata
!= '-')
1621 if (strncmp(metadata
+1, devnm
, l
) != 0)
1623 if (metadata
[l
+1] != '/')
1628 int metadata_subdev_matches(char *metadata
, char *devnm
)
1630 /* Check if 'devnm' is the subdev named in 'metadata'
1632 * /containername/subdev or
1633 * -containername/subdev
1636 if (*metadata
!= '/' && *metadata
!= '-')
1638 sl
= strchr(metadata
+1, '/');
1641 if (strcmp(sl
+1, devnm
) == 0)
1646 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1648 if (mdstat
->metadata_version
== NULL
||
1649 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1650 !metadata_container_matches(mdstat
->metadata_version
+9, container
))
1656 int is_subarray_active(char *subarray
, char *container
)
1658 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1659 struct mdstat_ent
*ent
;
1661 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1662 if (is_container_member(ent
, container
))
1663 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1666 free_mdstat(mdstat
);
1671 /* open_subarray - opens a subarray in a container
1672 * @dev: container device name
1673 * @st: empty supertype
1674 * @quiet: block reporting errors flag
1676 * On success returns an fd to a container and fills in *st
1678 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1681 struct mdinfo
*info
;
1685 fd
= open(dev
, O_RDWR
|O_EXCL
);
1688 pr_err("Couldn't open %s, aborting\n",
1693 _devnm
= fd2devnm(fd
);
1694 if (_devnm
== NULL
) {
1696 pr_err("Failed to determine device number for %s\n",
1700 strcpy(st
->devnm
, _devnm
);
1702 mdi
= sysfs_read(fd
, st
->devnm
, GET_VERSION
|GET_LEVEL
);
1705 pr_err("Failed to read sysfs for %s\n",
1710 if (mdi
->array
.level
!= UnSet
) {
1712 pr_err("%s is not a container\n", dev
);
1716 st
->ss
= version_to_superswitch(mdi
->text_version
);
1719 pr_err("Operation not supported for %s metadata\n",
1724 if (st
->devnm
[0] == 0) {
1726 pr_err("Failed to allocate device name\n");
1730 if (!st
->ss
->load_container
) {
1732 pr_err("%s is not a container\n", dev
);
1736 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1738 pr_err("Failed to load metadata for %s\n",
1743 info
= st
->ss
->container_content(st
, subarray
);
1746 pr_err("Failed to find subarray-%s in %s\n",
1756 st
->ss
->free_super(st
);
1769 int add_disk(int mdfd
, struct supertype
*st
,
1770 struct mdinfo
*sra
, struct mdinfo
*info
)
1772 /* Add a device to an array, in one of 2 ways. */
1775 if (st
->ss
->external
) {
1776 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1777 info
->recovery_start
= MaxSector
;
1779 info
->recovery_start
= 0;
1780 rv
= sysfs_add_disk(sra
, info
, 0);
1783 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1787 sd2
= xmalloc(sizeof(*sd2
));
1789 sd2
->next
= sra
->devs
;
1794 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1798 int remove_disk(int mdfd
, struct supertype
*st
,
1799 struct mdinfo
*sra
, struct mdinfo
*info
)
1803 /* Remove the disk given by 'info' from the array */
1804 if (st
->ss
->external
)
1805 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1807 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1812 int hot_remove_disk(int mdfd
, unsigned long dev
, int force
)
1814 int cnt
= force
? 500 : 5;
1817 /* HOT_REMOVE_DISK can fail with EBUSY if there are
1818 * outstanding IO requests to the device.
1819 * In this case, it can be helpful to wait a little while,
1820 * up to 5 seconds if 'force' is set, or 50 msec if not.
1822 while ((ret
= ioctl(mdfd
, HOT_REMOVE_DISK
, dev
)) == -1 &&
1830 int sys_hot_remove_disk(int statefd
, int force
)
1832 int cnt
= force
? 500 : 5;
1835 while ((ret
= write(statefd
, "remove", 6)) == -1 &&
1839 return ret
== 6 ? 0 : -1;
1842 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1844 /* Initialise kernel's knowledge of array.
1845 * This varies between externally managed arrays
1848 mdu_array_info_t inf
;
1851 if (st
->ss
->external
)
1852 return sysfs_set_array(info
, 9003);
1854 memset(&inf
, 0, sizeof(inf
));
1855 inf
.major_version
= info
->array
.major_version
;
1856 inf
.minor_version
= info
->array
.minor_version
;
1857 rv
= md_set_array_info(mdfd
, &inf
);
1862 unsigned long long min_recovery_start(struct mdinfo
*array
)
1864 /* find the minimum recovery_start in an array for metadata
1865 * formats that only record per-array recovery progress instead
1868 unsigned long long recovery_start
= MaxSector
;
1871 for (d
= array
->devs
; d
; d
= d
->next
)
1872 recovery_start
= min(recovery_start
, d
->recovery_start
);
1874 return recovery_start
;
1877 int mdmon_pid(char *devnm
)
1884 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devnm
);
1886 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1890 n
= read(fd
, pid
, 9);
1897 int mdmon_running(char *devnm
)
1899 int pid
= mdmon_pid(devnm
);
1902 if (kill(pid
, 0) == 0)
1907 int start_mdmon(char *devnm
)
1921 if (check_env("MDADM_NO_MDMON"))
1923 if (continue_via_systemd(devnm
, MDMON_SERVICE
))
1926 /* That failed, try running mdmon directly */
1927 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
1931 sl
= strrchr(pathbuf
, '/');
1936 strcpy(sl
, "mdmon");
1943 for (i
= 0; paths
[i
]; i
++)
1945 execl(paths
[i
], paths
[i
],
1949 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
1951 default: /* parent - good */
1952 pid
= wait(&status
);
1953 if (pid
< 0 || status
!= 0) {
1954 pr_err("failed to launch mdmon. Array remains readonly\n");
1961 __u32
random32(void)
1964 int rfd
= open("/dev/urandom", O_RDONLY
);
1965 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1972 void random_uuid(__u8
*buf
)
1977 fd
= open("/dev/urandom", O_RDONLY
);
1980 len
= read(fd
, buf
, 16);
1988 for (i
= 0; i
< 4; i
++)
1993 int flush_metadata_updates(struct supertype
*st
)
1997 st
->update_tail
= NULL
;
2001 sfd
= connect_monitor(st
->container_devnm
);
2005 while (st
->updates
) {
2006 struct metadata_update
*mu
= st
->updates
;
2007 st
->updates
= mu
->next
;
2009 send_message(sfd
, mu
, 0);
2017 st
->update_tail
= NULL
;
2021 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
2024 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
2029 mu
->space_list
= NULL
;
2031 *st
->update_tail
= mu
;
2032 st
->update_tail
= &mu
->next
;
2036 /* tinyc doesn't optimize this check in ioctl.h out ... */
2037 unsigned int __invalid_size_argument_for_IOC
= 0;
2040 /* Pick all spares matching given criteria from a container
2041 * if min_size == 0 do not check size
2042 * if domlist == NULL do not check domains
2043 * if spare_group given add it to domains of each spare
2044 * metadata allows to test domains using metadata of destination array */
2045 struct mdinfo
*container_choose_spares(struct supertype
*st
,
2046 struct spare_criteria
*criteria
,
2047 struct domainlist
*domlist
,
2049 const char *metadata
, int get_one
)
2051 struct mdinfo
*d
, **dp
, *disks
= NULL
;
2053 /* get list of all disks in container */
2054 if (st
->ss
->getinfo_super_disks
)
2055 disks
= st
->ss
->getinfo_super_disks(st
);
2059 /* find spare devices on the list */
2061 disks
->array
.spare_disks
= 0;
2065 if (d
->disk
.state
== 0) {
2066 /* check if size is acceptable */
2067 unsigned long long dev_size
;
2068 unsigned int dev_sector_size
;
2070 int sector_size_valid
= 0;
2072 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
2074 if (!criteria
->min_size
||
2075 (dev_size_from_id(dev
, &dev_size
) &&
2076 dev_size
>= criteria
->min_size
))
2079 if (!criteria
->sector_size
||
2080 (dev_sector_size_from_id(dev
, &dev_sector_size
) &&
2081 criteria
->sector_size
== dev_sector_size
))
2082 sector_size_valid
= 1;
2084 found
= size_valid
&& sector_size_valid
;
2086 /* check if domain matches */
2087 if (found
&& domlist
) {
2088 struct dev_policy
*pol
= devid_policy(dev
);
2090 pol_add(&pol
, pol_domain
,
2092 if (domain_test(domlist
, pol
, metadata
) != 1)
2094 dev_policy_free(pol
);
2099 disks
->array
.spare_disks
++;
2113 /* Checks if paths point to the same device
2114 * Returns 0 if they do.
2115 * Returns 1 if they don't.
2116 * Returns -1 if something went wrong,
2117 * e.g. paths are empty or the files
2118 * they point to don't exist */
2119 int compare_paths (char* path1
, char* path2
)
2121 struct stat st1
,st2
;
2123 if (path1
== NULL
|| path2
== NULL
)
2125 if (stat(path1
,&st1
) != 0)
2127 if (stat(path2
,&st2
) != 0)
2129 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
))
2134 /* Make sure we can open as many devices as needed */
2135 void enable_fds(int devices
)
2137 unsigned int fds
= 20 + devices
;
2139 if (getrlimit(RLIMIT_NOFILE
, &lim
) != 0 || lim
.rlim_cur
>= fds
)
2141 if (lim
.rlim_max
< fds
)
2144 setrlimit(RLIMIT_NOFILE
, &lim
);
2147 /* Close all opened descriptors if needed and redirect
2148 * streams to /dev/null.
2149 * For debug purposed, leave STDOUT and STDERR untouched
2151 * 1- if any error occurred
2154 void manage_fork_fds(int close_all
)
2157 struct dirent
*dirent
;
2160 open("/dev/null", O_RDWR
);
2170 dir
= opendir("/proc/self/fd");
2172 pr_err("Cannot open /proc/self/fd directory.\n");
2175 for (dirent
= readdir(dir
); dirent
; dirent
= readdir(dir
)) {
2178 if ((strcmp(dirent
->d_name
, ".") == 0) ||
2179 (strcmp(dirent
->d_name
, "..")) == 0)
2182 fd
= strtol(dirent
->d_name
, NULL
, 10);
2188 /* In a systemd/udev world, it is best to get systemd to
2189 * run daemon rather than running in the background.
2191 * 1- if systemd service has been started
2194 int continue_via_systemd(char *devnm
, char *service_name
)
2199 /* Simply return that service cannot be started */
2200 if (check_env("MDADM_NO_SYSTEMCTL"))
2205 snprintf(pathbuf
, sizeof(pathbuf
),
2206 "%s@%s.service", service_name
, devnm
);
2207 status
= execl("/usr/bin/systemctl", "systemctl", "restart",
2209 status
= execl("/bin/systemctl", "systemctl", "restart",
2212 case -1: /* Just do it ourselves. */
2214 default: /* parent - good */
2215 pid
= wait(&status
);
2216 if (pid
>= 0 && status
== 0)
2224 /* This is based on similar function in systemd. */
2226 /* statfs.f_type is signed long on s390x and MIPS, causing all
2227 sorts of sign extension problems with RAMFS_MAGIC being
2228 defined as 0x858458f6 */
2229 return statfs("/", &s
) >= 0 &&
2230 ((unsigned long)s
.f_type
== TMPFS_MAGIC
||
2231 ((unsigned long)s
.f_type
& 0xFFFFFFFFUL
) ==
2232 ((unsigned long)RAMFS_MAGIC
& 0xFFFFFFFFUL
));
2235 void reopen_mddev(int mdfd
)
2237 /* Re-open without any O_EXCL, but keep
2242 devnm
= fd2devnm(mdfd
);
2244 fd
= open_dev(devnm
);
2245 if (fd
>= 0 && fd
!= mdfd
)
2249 static struct cmap_hooks
*cmap_hooks
= NULL
;
2250 static int is_cmap_hooks_ready
= 0;
2252 void set_cmap_hooks(void)
2254 cmap_hooks
= xmalloc(sizeof(struct cmap_hooks
));
2255 cmap_hooks
->cmap_handle
= dlopen("libcmap.so.4", RTLD_NOW
| RTLD_LOCAL
);
2256 if (!cmap_hooks
->cmap_handle
)
2259 cmap_hooks
->initialize
=
2260 dlsym(cmap_hooks
->cmap_handle
, "cmap_initialize");
2261 cmap_hooks
->get_string
=
2262 dlsym(cmap_hooks
->cmap_handle
, "cmap_get_string");
2263 cmap_hooks
->finalize
= dlsym(cmap_hooks
->cmap_handle
, "cmap_finalize");
2265 if (!cmap_hooks
->initialize
|| !cmap_hooks
->get_string
||
2266 !cmap_hooks
->finalize
)
2267 dlclose(cmap_hooks
->cmap_handle
);
2269 is_cmap_hooks_ready
= 1;
2272 int get_cluster_name(char **cluster_name
)
2275 cmap_handle_t handle
;
2277 if (!is_cmap_hooks_ready
)
2280 rv
= cmap_hooks
->initialize(&handle
);
2284 rv
= cmap_hooks
->get_string(handle
, "totem.cluster_name", cluster_name
);
2286 free(*cluster_name
);
2293 cmap_hooks
->finalize(handle
);
2298 void set_dlm_hooks(void)
2300 dlm_hooks
= xmalloc(sizeof(struct dlm_hooks
));
2301 dlm_hooks
->dlm_handle
= dlopen("libdlm_lt.so.3", RTLD_NOW
| RTLD_LOCAL
);
2302 if (!dlm_hooks
->dlm_handle
)
2305 dlm_hooks
->open_lockspace
=
2306 dlsym(dlm_hooks
->dlm_handle
, "dlm_open_lockspace");
2307 dlm_hooks
->create_lockspace
=
2308 dlsym(dlm_hooks
->dlm_handle
, "dlm_create_lockspace");
2309 dlm_hooks
->release_lockspace
=
2310 dlsym(dlm_hooks
->dlm_handle
, "dlm_release_lockspace");
2311 dlm_hooks
->ls_lock
= dlsym(dlm_hooks
->dlm_handle
, "dlm_ls_lock");
2312 dlm_hooks
->ls_unlock_wait
=
2313 dlsym(dlm_hooks
->dlm_handle
, "dlm_ls_unlock_wait");
2314 dlm_hooks
->ls_get_fd
= dlsym(dlm_hooks
->dlm_handle
, "dlm_ls_get_fd");
2315 dlm_hooks
->dispatch
= dlsym(dlm_hooks
->dlm_handle
, "dlm_dispatch");
2317 if (!dlm_hooks
->open_lockspace
|| !dlm_hooks
->create_lockspace
||
2318 !dlm_hooks
->ls_lock
|| !dlm_hooks
->ls_unlock_wait
||
2319 !dlm_hooks
->release_lockspace
|| !dlm_hooks
->ls_get_fd
||
2320 !dlm_hooks
->dispatch
)
2321 dlclose(dlm_hooks
->dlm_handle
);
2323 is_dlm_hooks_ready
= 1;
2326 void set_hooks(void)
2332 int zero_disk_range(int fd
, unsigned long long sector
, size_t count
)
2338 size_t len
= count
* 512;
2341 fd_zero
= open("/dev/zero", O_RDONLY
);
2343 pr_err("Cannot open /dev/zero\n");
2347 if (lseek64(fd
, sector
* 512, SEEK_SET
) < 0) {
2349 pr_err("Failed to seek offset for zeroing\n");
2353 addr
= mmap(NULL
, len
, PROT_READ
, MAP_PRIVATE
, fd_zero
, 0);
2355 if (addr
== MAP_FAILED
) {
2357 pr_err("Mapping /dev/zero failed\n");
2362 n
= write(fd
, addr
+ written
, len
- written
);
2367 pr_err("Zeroing disk range failed\n");
2371 } while (written
!= len
);