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>
33 #include <linux/magic.h>
40 typedef uint64_t cmap_handle_t
;
43 #include <corosync/cmap.h>
48 * following taken from linux/blkpg.h because they aren't
49 * anywhere else and it isn't safe to #include linux/ * stuff.
52 #define BLKPG _IO(0x12,105)
54 /* The argument structure */
55 struct blkpg_ioctl_arg
{
62 /* The subfunctions (for the op field) */
63 #define BLKPG_ADD_PARTITION 1
64 #define BLKPG_DEL_PARTITION 2
66 /* Sizes of name fields. Unused at present. */
67 #define BLKPG_DEVNAMELTH 64
68 #define BLKPG_VOLNAMELTH 64
70 /* The data structure for ADD_PARTITION and DEL_PARTITION */
71 struct blkpg_partition
{
72 long long start
; /* starting offset in bytes */
73 long long length
; /* length in bytes */
74 int pno
; /* partition number */
75 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
76 to be used in kernel messages */
77 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
82 /* Force a compilation error if condition is true */
83 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
85 /* Force a compilation error if condition is true, but also produce a
86 result (of value 0 and type size_t), so the expression can be used
87 e.g. in a structure initializer (or where-ever else comma expressions
89 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
92 * Parse a 128 bit uuid in 4 integers
93 * format is 32 hexx nibbles with options :.<space> separator
94 * If not exactly 32 hex digits are found, return 0
97 int parse_uuid(char *str
, int uuid
[4])
99 int hit
= 0; /* number of Hex digIT */
102 for (i
= 0; i
< 4; i
++)
105 while ((c
= *str
++) != 0) {
107 if (c
>= '0' && c
<= '9')
109 else if (c
>= 'a' && c
<= 'f')
111 else if (c
>= 'A' && c
<= 'F')
113 else if (strchr(":. -", c
))
129 * Get the md version number.
130 * We use the RAID_VERSION ioctl if it is supported
131 * If not, but we have a block device with major '9', we assume
134 * Return version number as 24 but number - assume version parts
138 int md_get_version(int fd
)
143 if (fstat(fd
, &stb
)<0)
145 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
148 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
149 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
152 if (major(stb
.st_rdev
) == MD_MAJOR
)
157 int get_linux_version()
161 int a
= 0, b
= 0,c
= 0;
166 a
= strtoul(cp
, &cp
, 10);
168 b
= strtoul(cp
+1, &cp
, 10);
170 c
= strtoul(cp
+1, &cp
, 10);
172 return (a
*1000000)+(b
*1000)+c
;
176 int mdadm_version(char *version
)
184 cp
= strchr(version
, '-');
185 if (!cp
|| *(cp
+1) != ' ' || *(cp
+2) != 'v')
188 a
= strtoul(cp
, &cp
, 10);
191 b
= strtoul(cp
+1, &cp
, 10);
193 c
= strtoul(cp
+1, &cp
, 10);
196 if (*cp
!= ' ' && *cp
!= '-')
198 return (a
*1000000)+(b
*1000)+c
;
201 unsigned long long parse_size(char *size
)
203 /* parse 'size' which should be a number optionally
204 * followed by 'K', 'M', or 'G'.
205 * Without a suffix, K is assumed.
206 * Number returned is in sectors (half-K)
207 * INVALID_SECTORS returned on error.
210 long long s
= strtoll(size
, &c
, 10);
224 s
*= 1024 * 1024 * 2;
226 case 's': /* sectors */
237 int parse_layout_10(char *layout
)
241 /* Parse the layout string for raid10 */
242 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
243 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
244 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
248 if (layout
[0] == 'n')
250 else if (layout
[0] == 'o')
251 rv
= 0x10000 + (copies
<<8) + 1;
253 rv
= 1 + (copies
<<8);
257 int parse_layout_faulty(char *layout
)
259 /* Parse the layout string for 'faulty' */
260 int ln
= strcspn(layout
, "0123456789");
261 char *m
= xstrdup(layout
);
264 mode
= map_name(faultylayout
, m
);
268 return mode
| (atoi(layout
+ln
)<< ModeShift
);
271 long parse_num(char *num
)
273 /* Either return a valid number, or -1 */
275 long rv
= strtol(num
, &c
, 10);
276 if (rv
< 0 || *c
|| !num
[0])
283 void remove_partitions(int fd
)
285 /* remove partitions from this block devices.
286 * This is used for components added to an array
288 #ifdef BLKPG_DEL_PARTITION
289 struct blkpg_ioctl_arg a
;
290 struct blkpg_partition p
;
292 a
.op
= BLKPG_DEL_PARTITION
;
294 a
.datalen
= sizeof(p
);
296 memset(a
.data
, 0, a
.datalen
);
297 for (p
.pno
= 0; p
.pno
< 16; p
.pno
++)
298 ioctl(fd
, BLKPG
, &a
);
302 int test_partition(int fd
)
304 /* Check if fd is a whole-disk or a partition.
305 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
306 * will return ENXIO on an invalid partition number.
308 struct blkpg_ioctl_arg a
;
309 struct blkpg_partition p
;
310 a
.op
= BLKPG_DEL_PARTITION
;
312 a
.datalen
= sizeof(p
);
314 memset(a
.data
, 0, a
.datalen
);
316 if (ioctl(fd
, BLKPG
, &a
) == 0)
317 /* Very unlikely, but not a partition */
319 if (errno
== ENXIO
|| errno
== ENOTTY
)
320 /* not a partition */
326 int test_partition_from_id(dev_t id
)
331 sprintf(buf
, "%d:%d", major(id
), minor(id
));
332 fd
= dev_open(buf
, O_RDONLY
);
335 rv
= test_partition(fd
);
340 int enough(int level
, int raid_disks
, int layout
, int clean
, char *avail
)
346 for (i
= 0; i
< raid_disks
; i
++)
347 avail_disks
+= !!avail
[i
];
351 /* This is the tricky one - we need to check
352 * which actual disks are present.
354 copies
= (layout
&255)* ((layout
>>8) & 255);
357 /* there must be one of the 'copies' form 'first' */
364 this = (this+1) % raid_disks
;
368 first
= (first
+(layout
&255)) % raid_disks
;
369 } while (first
!= 0);
372 case LEVEL_MULTIPATH
:
373 return avail_disks
>= 1;
376 return avail_disks
== raid_disks
;
378 return avail_disks
>= 1;
380 if (avail_disks
== raid_disks
- 1 &&
381 !avail
[raid_disks
- 1])
382 /* If just the parity device is missing, then we
383 * have enough, even if not clean
389 return avail_disks
>= raid_disks
-1;
391 return avail_disks
>= raid_disks
;
394 return avail_disks
>= raid_disks
-2;
396 return avail_disks
>= raid_disks
;
402 int enough_fd(int fd
)
404 struct mdu_array_info_s array
;
405 struct mdu_disk_info_s disk
;
409 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
410 array
.raid_disks
<= 0)
412 avail
= xcalloc(array
.raid_disks
, 1);
413 for (i
= 0; i
< MAX_DISKS
&& array
.nr_disks
> 0; i
++) {
415 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
417 if (disk
.major
== 0 && disk
.minor
== 0)
421 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
423 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
425 avail
[disk
.raid_disk
] = 1;
427 /* This is used on an active array, so assume it is clean */
428 rv
= enough(array
.level
, array
.raid_disks
, array
.layout
,
434 const int uuid_zero
[4] = { 0, 0, 0, 0 };
436 int same_uuid(int a
[4], int b
[4], int swapuuid
)
439 /* parse uuids are hostendian.
440 * uuid's from some superblocks are big-ending
441 * if there is a difference, we need to swap..
443 unsigned char *ac
= (unsigned char *)a
;
444 unsigned char *bc
= (unsigned char *)b
;
446 for (i
= 0; i
< 16; i
+= 4) {
447 if (ac
[i
+0] != bc
[i
+3] ||
448 ac
[i
+1] != bc
[i
+2] ||
449 ac
[i
+2] != bc
[i
+1] ||
464 void copy_uuid(void *a
, int b
[4], int swapuuid
)
467 /* parse uuids are hostendian.
468 * uuid's from some superblocks are big-ending
469 * if there is a difference, we need to swap..
471 unsigned char *ac
= (unsigned char *)a
;
472 unsigned char *bc
= (unsigned char *)b
;
474 for (i
= 0; i
< 16; i
+= 4) {
484 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
491 copy_uuid(uuid
, id
, swap
);
492 for (i
= 0; i
< 4; i
++) {
495 for (j
= 3; j
>= 0; j
--) {
496 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
504 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
506 // dirty hack to work around an issue with super1 superblocks...
507 // super1 superblocks need swapuuid set in order for assembly to
508 // work, but can't have it set if we want this printout to match
509 // all the other uuid printouts in super1.c, so we force swapuuid
510 // to 1 to make our printout match the rest of super1
511 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
515 int check_ext2(int fd
, char *name
)
518 * Check for an ext2fs file system.
519 * Superblock is always 1K at 1K offset
521 * s_magic is le16 at 56 == 0xEF53
522 * report mtime - le32 at 44
524 * logblksize - le32 at 24
526 unsigned char sb
[1024];
528 unsigned long long size
;
530 if (lseek(fd
, 1024,0)!= 1024)
532 if (read(fd
, sb
, 1024)!= 1024)
534 if (sb
[56] != 0x53 || sb
[57] != 0xef)
537 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
538 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
539 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
541 pr_err("%s appears to contain an ext2fs file system\n",
543 cont_err("size=%lluK mtime=%s", size
, ctime(&mtime
));
547 int check_reiser(int fd
, char *name
)
550 * superblock is at 64K
552 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
555 unsigned char sb
[1024];
556 unsigned long long size
;
557 if (lseek(fd
, 64*1024, 0) != 64*1024)
559 if (read(fd
, sb
, 1024) != 1024)
561 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
562 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
564 pr_err("%s appears to contain a reiserfs file system\n",name
);
565 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
566 cont_err("size = %lluK\n", size
*4);
571 int check_raid(int fd
, char *name
)
576 struct supertype
*st
= guess_super(fd
);
580 st
->ss
->load_super(st
, fd
, name
);
581 /* Looks like a raid array .. */
582 pr_err("%s appears to be part of a raid array:\n",
584 st
->ss
->getinfo_super(st
, &info
, NULL
);
585 st
->ss
->free_super(st
);
586 crtime
= info
.array
.ctime
;
587 level
= map_num(pers
, info
.array
.level
);
588 if (!level
) level
= "-unknown-";
589 cont_err("level=%s devices=%d ctime=%s",
590 level
, info
.array
.raid_disks
, ctime(&crtime
));
598 for (i
= 0; i
< 5; i
++) {
600 fprintf(stderr
, "%s%s", mesg
, add
);
602 if (fgets(buf
, 100, stdin
)==NULL
)
604 if (buf
[0]=='y' || buf
[0]=='Y')
606 if (buf
[0]=='n' || buf
[0]=='N')
610 pr_err("assuming 'no'\n");
613 #endif /* MDASSEMBLE */
615 int is_standard(char *dev
, int *nump
)
617 /* tests if dev is a "standard" md dev name.
618 * i.e if the last component is "/dNN" or "/mdNN",
619 * where NN is a string of digits
620 * Returns 1 if a partitionable standard,
621 * -1 if non-partitonable,
622 * 0 if not a standard name.
624 char *d
= strrchr(dev
, '/');
629 if (strncmp(d
, "/d",2) == 0)
630 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
631 else if (strncmp(d
, "/md_d", 5) == 0)
632 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
633 else if (strncmp(d
, "/md", 3) == 0)
634 d
+= 3, type
= -1; /* /dev/mdN */
635 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
636 d
+= 1, type
= -1; /* /dev/md/N */
646 if (nump
) *nump
= num
;
651 unsigned long calc_csum(void *super
, int bytes
)
653 unsigned long long newcsum
= 0;
656 unsigned int *superc
= (unsigned int*) super
;
658 for(i
= 0; i
< bytes
/4; i
++)
659 newcsum
+= superc
[i
];
660 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
662 /* The in-kernel checksum calculation is always 16bit on
663 * the alpha, though it is 32 bit on i386...
664 * I wonder what it is elsewhere... (it uses an API in
665 * a way that it shouldn't).
667 csum
= (csum
& 0xffff) + (csum
>> 16);
668 csum
= (csum
& 0xffff) + (csum
>> 16);
674 char *human_size(long long bytes
)
678 /* We convert bytes to either centi-M{ega,ibi}bytes or
679 * centi-G{igi,ibi}bytes, with appropriate rounding,
680 * and then print 1/100th of those as a decimal.
681 * We allow upto 2048Megabytes before converting to
682 * gigabytes, as that shows more precision and isn't
683 * too large a number.
684 * Terabytes are not yet handled.
687 if (bytes
< 5000*1024)
689 else if (bytes
< 2*1024LL*1024LL*1024LL) {
690 long cMiB
= (bytes
* 200LL / (1LL<<20) + 1) / 2;
691 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
692 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
693 cMiB
/100 , cMiB
% 100,
696 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) / 2;
697 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
698 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
699 cGiB
/100 , cGiB
% 100,
705 char *human_size_brief(long long bytes
, int prefix
)
709 /* We convert bytes to either centi-M{ega,ibi}bytes or
710 * centi-G{igi,ibi}bytes, with appropriate rounding,
711 * and then print 1/100th of those as a decimal.
712 * We allow upto 2048Megabytes before converting to
713 * gigabytes, as that shows more precision and isn't
714 * too large a number.
715 * Terabytes are not yet handled.
717 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
718 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
721 if (bytes
< 5000*1024)
723 else if (prefix
== IEC
) {
724 if (bytes
< 2*1024LL*1024LL*1024LL) {
725 long cMiB
= (bytes
* 200LL / (1LL<<20) +1) /2;
726 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
727 cMiB
/100 , cMiB
% 100);
729 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) /2;
730 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
731 cGiB
/100 , cGiB
% 100);
734 else if (prefix
== JEDEC
) {
735 if (bytes
< 2*1024LL*1024LL*1024LL) {
736 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
737 snprintf(buf
, sizeof(buf
), "%ld.%02ldMB",
740 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
741 snprintf(buf
, sizeof(buf
), "%ld.%02ldGB",
742 cGB
/100 , cGB
% 100);
751 void print_r10_layout(int layout
)
753 int near
= layout
& 255;
754 int far
= (layout
>> 8) & 255;
755 int offset
= (layout
&0x10000);
759 printf("%s near=%d", sep
, near
);
763 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
765 printf("NO REDUNDANCY");
769 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
770 int chunksize
, unsigned long long devsize
)
774 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
775 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
778 int get_data_disks(int level
, int layout
, int raid_disks
)
782 case 0: data_disks
= raid_disks
;
784 case 1: data_disks
= 1;
787 case 5: data_disks
= raid_disks
- 1;
789 case 6: data_disks
= raid_disks
- 2;
791 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
798 int devnm2devid(char *devnm
)
800 /* First look in /sys/block/$DEVNM/dev for %d:%d
801 * If that fails, try parsing out a number
808 sprintf(path
, "/sys/block/%s/dev", devnm
);
809 fd
= open(path
, O_RDONLY
);
812 int n
= read(fd
, buf
, sizeof(buf
));
816 if (n
> 0 && sscanf(buf
, "%d:%d\n", &mjr
, &mnr
) == 2)
817 return makedev(mjr
, mnr
);
819 if (strncmp(devnm
, "md_d", 4) == 0 &&
821 (mnr
= strtoul(devnm
+4, &ep
, 10)) >= 0 &&
822 ep
> devnm
&& *ep
== 0)
823 return makedev(get_mdp_major(), mnr
<< MdpMinorShift
);
825 if (strncmp(devnm
, "md", 2) == 0 &&
827 (mnr
= strtoul(devnm
+2, &ep
, 10)) >= 0 &&
828 ep
> devnm
&& *ep
== 0)
829 return makedev(MD_MAJOR
, mnr
);
834 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
835 char *get_md_name(char *devnm
)
837 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
838 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
840 static char devname
[50];
842 dev_t rdev
= devnm2devid(devnm
);
847 if (strncmp(devnm
, "md_", 3) == 0) {
848 snprintf(devname
, sizeof(devname
), "/dev/md/%s",
850 if (stat(devname
, &stb
) == 0
851 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
852 && (stb
.st_rdev
== rdev
))
855 snprintf(devname
, sizeof(devname
), "/dev/%s", devnm
);
856 if (stat(devname
, &stb
) == 0
857 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
858 && (stb
.st_rdev
== rdev
))
861 snprintf(devname
, sizeof(devname
), "/dev/md/%s", devnm
+2);
862 if (stat(devname
, &stb
) == 0
863 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
864 && (stb
.st_rdev
== rdev
))
867 dn
= map_dev(major(rdev
), minor(rdev
), 0);
870 snprintf(devname
, sizeof(devname
), "/dev/.tmp.%s", devnm
);
871 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
875 if (stat(devname
, &stb
) == 0
876 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
877 && (stb
.st_rdev
== rdev
))
883 void put_md_name(char *name
)
885 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
888 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
890 int get_maj_min(char *dev
, int *major
, int *minor
)
893 *major
= strtoul(dev
, &e
, 0);
894 return (e
> dev
&& *e
== ':' && e
[1] &&
895 (*minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
899 int dev_open(char *dev
, int flags
)
901 /* like 'open', but if 'dev' matches %d:%d, create a temp
902 * block device and open that
912 if (get_maj_min(dev
, &major
, &minor
)) {
913 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
914 (int)getpid(), major
, minor
);
915 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
916 fd
= open(devname
, flags
);
920 /* Try /tmp as /dev appear to be read-only */
921 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
922 (int)getpid(), major
, minor
);
923 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
924 fd
= open(devname
, flags
);
929 fd
= open(dev
, flags
);
933 int open_dev_flags(char *devnm
, int flags
)
938 devid
= devnm2devid(devnm
);
939 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
940 return dev_open(buf
, flags
);
943 int open_dev(char *devnm
)
945 return open_dev_flags(devnm
, O_RDONLY
);
948 int open_dev_excl(char *devnm
)
953 int devid
= devnm2devid(devnm
);
956 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
957 for (i
= 0 ; i
< 25 ; i
++) {
958 int fd
= dev_open(buf
, flags
|O_EXCL
);
961 if (errno
== EACCES
&& flags
== O_RDWR
) {
974 int same_dev(char *one
, char *two
)
976 struct stat st1
, st2
;
977 if (stat(one
, &st1
) != 0)
979 if (stat(two
, &st2
) != 0)
981 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
983 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
985 return st1
.st_rdev
== st2
.st_rdev
;
988 void wait_for(char *dev
, int fd
)
991 struct stat stb_want
;
994 if (fstat(fd
, &stb_want
) != 0 ||
995 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
998 for (i
= 0 ; i
< 25 ; i
++) {
1000 if (stat(dev
, &stb
) == 0 &&
1001 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1002 (stb
.st_rdev
== stb_want
.st_rdev
))
1009 dprintf("timeout waiting for %s\n", dev
);
1012 struct superswitch
*superlist
[] =
1015 &super_ddf
, &super_imsm
,
1019 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1021 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
1023 mdu_array_info_t array
;
1026 struct supertype
*st
= NULL
;
1031 char *subarray
= NULL
;
1032 char container
[32] = "";
1034 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
1037 vers
= sra
->array
.major_version
;
1038 minor
= sra
->array
.minor_version
;
1039 verstr
= sra
->text_version
;
1041 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1042 array
.major_version
= array
.minor_version
= 0;
1043 vers
= array
.major_version
;
1044 minor
= array
.minor_version
;
1049 sprintf(version
, "%d.%d", vers
, minor
);
1052 if (minor
== -2 && is_subarray(verstr
)) {
1053 char *dev
= verstr
+1;
1055 subarray
= strchr(dev
, '/');
1058 subarray
= xstrdup(subarray
);
1060 strcpy(container
, dev
);
1063 sra
= sysfs_read(-1, container
, GET_VERSION
);
1064 if (sra
&& sra
->text_version
[0])
1065 verstr
= sra
->text_version
;
1067 verstr
= "-no-metadata-";
1070 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1071 st
= superlist
[i
]->match_metadata_desc(verstr
);
1078 *subarrayp
= subarray
;
1079 strcpy(st
->container_devnm
, container
);
1080 strcpy(st
->devnm
, fd2devnm(fd
));
1086 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1088 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1093 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1094 fd
= dev_open(buf
, O_RDONLY
);
1097 if (get_dev_size(fd
, NULL
, size
)) {
1105 struct supertype
*dup_super(struct supertype
*orig
)
1107 struct supertype
*st
;
1111 st
= xcalloc(1, sizeof(*st
));
1113 st
->max_devs
= orig
->max_devs
;
1114 st
->minor_version
= orig
->minor_version
;
1115 st
->ignore_hw_compat
= orig
->ignore_hw_compat
;
1116 st
->data_offset
= orig
->data_offset
;
1122 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1124 /* try each load_super to find the best match,
1125 * and return the best superswitch
1127 struct superswitch
*ss
;
1128 struct supertype
*st
;
1129 time_t besttime
= 0;
1133 st
= xcalloc(1, sizeof(*st
));
1134 st
->container_devnm
[0] = 0;
1136 for (i
= 0 ; superlist
[i
]; i
++) {
1139 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1141 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1143 memset(st
, 0, sizeof(*st
));
1144 st
->ignore_hw_compat
= 1;
1145 rv
= ss
->load_super(st
, fd
, NULL
);
1148 st
->ss
->getinfo_super(st
, &info
, NULL
);
1149 if (bestsuper
== -1 ||
1150 besttime
< info
.array
.ctime
) {
1152 besttime
= info
.array
.ctime
;
1157 if (bestsuper
!= -1) {
1159 memset(st
, 0, sizeof(*st
));
1160 st
->ignore_hw_compat
= 1;
1161 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1163 superlist
[bestsuper
]->free_super(st
);
1171 /* Return size of device in bytes */
1172 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1174 unsigned long long ldsize
;
1177 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1178 ldsize
= (unsigned long long)st
.st_size
;
1181 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1184 unsigned long dsize
;
1185 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1190 pr_err("Cannot get size of %s: %s\b",
1191 dname
, strerror(errno
));
1199 /* Return true if this can only be a container, not a member device.
1200 * i.e. is and md device and size is zero
1202 int must_be_container(int fd
)
1204 unsigned long long size
;
1205 if (md_get_version(fd
) < 0)
1207 if (get_dev_size(fd
, NULL
, &size
) == 0)
1214 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1215 * Returns: 1 if successful
1216 * -1 for unknown partition type
1217 * 0 for other errors
1219 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1222 unsigned char empty_gpt_entry
[16]= {0};
1223 struct GPT_part_entry
*part
;
1225 unsigned long long curr_part_end
;
1226 unsigned all_partitions
, entry_size
;
1231 BUILD_BUG_ON(sizeof(gpt
) != 512);
1232 /* skip protective MBR */
1233 lseek(fd
, 512, SEEK_SET
);
1234 /* read GPT header */
1235 if (read(fd
, &gpt
, 512) != 512)
1238 /* get the number of partition entries and the entry size */
1239 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1240 entry_size
= __le32_to_cpu(gpt
.part_size
);
1242 /* Check GPT signature*/
1243 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1247 if (all_partitions
> 1024 ||
1248 entry_size
> sizeof(buf
))
1251 part
= (struct GPT_part_entry
*)buf
;
1253 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1254 /* read partition entry */
1255 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1258 /* is this valid partition? */
1259 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1260 /* check the last lba for the current partition */
1261 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1262 if (curr_part_end
> *endofpart
)
1263 *endofpart
= curr_part_end
;
1270 /* Sets endofpart parameter to the last block used by the last partition on the device.
1271 * Returns: 1 if successful
1272 * -1 for unknown partition type
1273 * 0 for other errors
1275 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1277 struct MBR boot_sect
;
1278 struct MBR_part_record
*part
;
1279 unsigned long long curr_part_end
;
1285 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1288 if (read(fd
, &boot_sect
, 512) != 512)
1291 /* check MBP signature */
1292 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1294 /* found the correct signature */
1295 part
= boot_sect
.parts
;
1297 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1298 /* check for GPT type */
1299 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1300 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1303 /* check the last used lba for the current partition */
1304 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1305 __le32_to_cpu(part
->blocks_num
);
1306 if (curr_part_end
> *endofpart
)
1307 *endofpart
= curr_part_end
;
1312 /* Unknown partition table */
1319 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1320 unsigned long long size
)
1323 * Check where the last partition ends
1325 unsigned long long endofpart
;
1328 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1329 /* There appears to be a partition table here */
1330 if (freesize
== 0) {
1331 /* partitions will not be visible in new device */
1332 pr_err("partition table exists on %s but will be lost or\n"
1333 " meaningless after creating array\n",
1336 } else if (endofpart
> freesize
) {
1337 /* last partition overlaps metadata */
1338 pr_err("metadata will over-write last partition on %s.\n",
1341 } else if (size
&& endofpart
> size
) {
1342 /* partitions will be truncated in new device */
1343 pr_err("array size is too small to cover all partitions on %s.\n",
1351 int open_container(int fd
)
1353 /* 'fd' is a block device. Find out if it is in use
1354 * by a container, and return an open fd on that container.
1365 if (fstat(fd
, &st
) != 0)
1367 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1368 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1369 e
= path
+ strlen(path
);
1371 dir
= opendir(path
);
1374 while ((de
= readdir(dir
))) {
1377 if (de
->d_name
[0] == '.')
1379 /* Need to make sure it is a container and not a volume */
1380 sprintf(e
, "/%s/md/metadata_version", de
->d_name
);
1381 dfd
= open(path
, O_RDONLY
);
1384 n
= read(dfd
, buf
, sizeof(buf
));
1386 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1389 if (strncmp(buf
, "external", 8) != 0 ||
1393 sprintf(e
, "/%s/dev", de
->d_name
);
1394 dfd
= open(path
, O_RDONLY
);
1397 n
= read(dfd
, buf
, sizeof(buf
));
1399 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1402 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1404 sprintf(buf
, "%d:%d", major
, minor
);
1405 dfd
= dev_open(buf
, O_RDONLY
);
1415 struct superswitch
*version_to_superswitch(char *vers
)
1419 for (i
= 0; superlist
[i
]; i
++) {
1420 struct superswitch
*ss
= superlist
[i
];
1422 if (strcmp(vers
, ss
->name
) == 0)
1429 int metadata_container_matches(char *metadata
, char *devnm
)
1431 /* Check if 'devnm' is the container named in 'metadata'
1433 * /containername/componentname or
1434 * -containername/componentname
1437 if (*metadata
!= '/' && *metadata
!= '-')
1440 if (strncmp(metadata
+1, devnm
, l
) != 0)
1442 if (metadata
[l
+1] != '/')
1447 int metadata_subdev_matches(char *metadata
, char *devnm
)
1449 /* Check if 'devnm' is the subdev named in 'metadata'
1451 * /containername/subdev or
1452 * -containername/subdev
1455 if (*metadata
!= '/' && *metadata
!= '-')
1457 sl
= strchr(metadata
+1, '/');
1460 if (strcmp(sl
+1, devnm
) == 0)
1465 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1467 if (mdstat
->metadata_version
== NULL
||
1468 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1469 !metadata_container_matches(mdstat
->metadata_version
+9, container
))
1475 int is_subarray_active(char *subarray
, char *container
)
1477 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1478 struct mdstat_ent
*ent
;
1480 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1481 if (is_container_member(ent
, container
))
1482 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1485 free_mdstat(mdstat
);
1490 /* open_subarray - opens a subarray in a container
1491 * @dev: container device name
1492 * @st: empty supertype
1493 * @quiet: block reporting errors flag
1495 * On success returns an fd to a container and fills in *st
1497 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1500 struct mdinfo
*info
;
1504 fd
= open(dev
, O_RDWR
|O_EXCL
);
1507 pr_err("Couldn't open %s, aborting\n",
1512 _devnm
= fd2devnm(fd
);
1513 if (_devnm
== NULL
) {
1515 pr_err("Failed to determine device number for %s\n",
1519 strcpy(st
->devnm
, _devnm
);
1521 mdi
= sysfs_read(fd
, st
->devnm
, GET_VERSION
|GET_LEVEL
);
1524 pr_err("Failed to read sysfs for %s\n",
1529 if (mdi
->array
.level
!= UnSet
) {
1531 pr_err("%s is not a container\n", dev
);
1535 st
->ss
= version_to_superswitch(mdi
->text_version
);
1538 pr_err("Operation not supported for %s metadata\n",
1543 if (st
->devnm
[0] == 0) {
1545 pr_err("Failed to allocate device name\n");
1549 if (!st
->ss
->load_container
) {
1551 pr_err("%s is not a container\n", dev
);
1555 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1557 pr_err("Failed to load metadata for %s\n",
1562 info
= st
->ss
->container_content(st
, subarray
);
1565 pr_err("Failed to find subarray-%s in %s\n",
1575 st
->ss
->free_super(st
);
1588 int add_disk(int mdfd
, struct supertype
*st
,
1589 struct mdinfo
*sra
, struct mdinfo
*info
)
1591 /* Add a device to an array, in one of 2 ways. */
1594 if (st
->ss
->external
) {
1595 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1596 info
->recovery_start
= MaxSector
;
1598 info
->recovery_start
= 0;
1599 rv
= sysfs_add_disk(sra
, info
, 0);
1602 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1606 sd2
= xmalloc(sizeof(*sd2
));
1608 sd2
->next
= sra
->devs
;
1614 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1618 int remove_disk(int mdfd
, struct supertype
*st
,
1619 struct mdinfo
*sra
, struct mdinfo
*info
)
1622 /* Remove the disk given by 'info' from the array */
1624 if (st
->ss
->external
)
1625 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1628 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1633 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1635 /* Initialise kernel's knowledge of array.
1636 * This varies between externally managed arrays
1639 int vers
= md_get_version(mdfd
);
1643 if (st
->ss
->external
)
1644 rv
= sysfs_set_array(info
, vers
);
1647 if ((vers
% 100) >= 1) { /* can use different versions */
1648 mdu_array_info_t inf
;
1649 memset(&inf
, 0, sizeof(inf
));
1650 inf
.major_version
= info
->array
.major_version
;
1651 inf
.minor_version
= info
->array
.minor_version
;
1652 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1654 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1658 unsigned long long min_recovery_start(struct mdinfo
*array
)
1660 /* find the minimum recovery_start in an array for metadata
1661 * formats that only record per-array recovery progress instead
1664 unsigned long long recovery_start
= MaxSector
;
1667 for (d
= array
->devs
; d
; d
= d
->next
)
1668 recovery_start
= min(recovery_start
, d
->recovery_start
);
1670 return recovery_start
;
1673 int mdmon_pid(char *devnm
)
1680 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devnm
);
1682 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1686 n
= read(fd
, pid
, 9);
1693 int mdmon_running(char *devnm
)
1695 int pid
= mdmon_pid(devnm
);
1698 if (kill(pid
, 0) == 0)
1703 int start_mdmon(char *devnm
)
1717 if (check_env("MDADM_NO_MDMON"))
1720 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
1724 sl
= strrchr(pathbuf
, '/');
1729 strcpy(sl
, "mdmon");
1733 /* First try to run systemctl */
1734 if (!check_env("MDADM_NO_SYSTEMCTL"))
1737 /* FIXME yuk. CLOSE_EXEC?? */
1739 for (i
= 3; skipped
< 20; i
++)
1745 /* Don't want to see error messages from
1746 * systemctl. If the service doesn't exist,
1747 * we start mdmon ourselves.
1750 open("/dev/null", O_WRONLY
);
1751 snprintf(pathbuf
, sizeof(pathbuf
), "mdmon@%s.service",
1753 status
= execl("/usr/bin/systemctl", "systemctl",
1756 status
= execl("/bin/systemctl", "systemctl", "start",
1759 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
1761 default: /* parent - good */
1762 pid
= wait(&status
);
1763 if (pid
>= 0 && status
== 0)
1767 /* That failed, try running mdmon directly */
1770 /* FIXME yuk. CLOSE_EXEC?? */
1772 for (i
= 3; skipped
< 20; i
++)
1778 for (i
= 0; paths
[i
]; i
++)
1780 execl(paths
[i
], paths
[i
],
1784 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
1786 default: /* parent - good */
1787 pid
= wait(&status
);
1788 if (pid
< 0 || status
!= 0) {
1789 pr_err("failed to launch mdmon. Array remains readonly\n");
1796 __u32
random32(void)
1799 int rfd
= open("/dev/urandom", O_RDONLY
);
1800 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1808 int flush_metadata_updates(struct supertype
*st
)
1812 st
->update_tail
= NULL
;
1816 sfd
= connect_monitor(st
->container_devnm
);
1820 while (st
->updates
) {
1821 struct metadata_update
*mu
= st
->updates
;
1822 st
->updates
= mu
->next
;
1824 send_message(sfd
, mu
, 0);
1832 st
->update_tail
= NULL
;
1836 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1839 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
1844 mu
->space_list
= NULL
;
1846 *st
->update_tail
= mu
;
1847 st
->update_tail
= &mu
->next
;
1849 #endif /* MDASSEMBLE */
1852 /* tinyc doesn't optimize this check in ioctl.h out ... */
1853 unsigned int __invalid_size_argument_for_IOC
= 0;
1856 int experimental(void)
1858 if (check_env("MDADM_EXPERIMENTAL"))
1861 pr_err("To use this feature MDADM_EXPERIMENTAL environment variable has to be defined.\n");
1866 /* Pick all spares matching given criteria from a container
1867 * if min_size == 0 do not check size
1868 * if domlist == NULL do not check domains
1869 * if spare_group given add it to domains of each spare
1870 * metadata allows to test domains using metadata of destination array */
1871 struct mdinfo
*container_choose_spares(struct supertype
*st
,
1872 unsigned long long min_size
,
1873 struct domainlist
*domlist
,
1875 const char *metadata
, int get_one
)
1877 struct mdinfo
*d
, **dp
, *disks
= NULL
;
1879 /* get list of all disks in container */
1880 if (st
->ss
->getinfo_super_disks
)
1881 disks
= st
->ss
->getinfo_super_disks(st
);
1885 /* find spare devices on the list */
1887 disks
->array
.spare_disks
= 0;
1891 if (d
->disk
.state
== 0) {
1892 /* check if size is acceptable */
1893 unsigned long long dev_size
;
1894 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
1897 (dev_size_from_id(dev
, &dev_size
) &&
1898 dev_size
>= min_size
))
1900 /* check if domain matches */
1901 if (found
&& domlist
) {
1902 struct dev_policy
*pol
= devid_policy(dev
);
1904 pol_add(&pol
, pol_domain
,
1906 if (domain_test(domlist
, pol
, metadata
) != 1)
1908 dev_policy_free(pol
);
1913 disks
->array
.spare_disks
++;
1927 /* Checks if paths point to the same device
1928 * Returns 0 if they do.
1929 * Returns 1 if they don't.
1930 * Returns -1 if something went wrong,
1931 * e.g. paths are empty or the files
1932 * they point to don't exist */
1933 int compare_paths (char* path1
, char* path2
)
1935 struct stat st1
,st2
;
1937 if (path1
== NULL
|| path2
== NULL
)
1939 if (stat(path1
,&st1
) != 0)
1941 if (stat(path2
,&st2
) != 0)
1943 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
))
1948 /* Make sure we can open as many devices as needed */
1949 void enable_fds(int devices
)
1951 unsigned int fds
= 20 + devices
;
1953 if (getrlimit(RLIMIT_NOFILE
, &lim
) != 0
1954 || lim
.rlim_cur
>= fds
)
1956 if (lim
.rlim_max
< fds
)
1959 setrlimit(RLIMIT_NOFILE
, &lim
);
1964 /* This is based on similar function in systemd. */
1966 /* statfs.f_type is signed long on s390x and MIPS, causing all
1967 sorts of sign extension problems with RAMFS_MAGIC being
1968 defined as 0x858458f6 */
1969 return statfs("/", &s
) >= 0 &&
1970 ((unsigned long)s
.f_type
== TMPFS_MAGIC
||
1971 ((unsigned long)s
.f_type
& 0xFFFFFFFFUL
) ==
1972 ((unsigned long)RAMFS_MAGIC
& 0xFFFFFFFFUL
));
1975 void reopen_mddev(int mdfd
)
1977 /* Re-open without any O_EXCL, but keep
1982 devnm
= fd2devnm(mdfd
);
1984 fd
= open_dev(devnm
);
1985 if (fd
>= 0 && fd
!= mdfd
)
1989 int get_cluster_name(char **cluster_name
)
1991 void *lib_handle
= NULL
;
1994 cmap_handle_t handle
;
1995 static int (*initialize
)(cmap_handle_t
*handle
);
1996 static int (*get_string
)(cmap_handle_t handle
,
1999 static int (*finalize
)(cmap_handle_t handle
);
2002 lib_handle
= dlopen("libcmap.so.4", RTLD_NOW
| RTLD_LOCAL
);
2006 initialize
= dlsym(lib_handle
, "cmap_initialize");
2010 get_string
= dlsym(lib_handle
, "cmap_get_string");
2014 finalize
= dlsym(lib_handle
, "cmap_finalize");
2018 rv
= initialize(&handle
);
2022 rv
= get_string(handle
, "totem.cluster_name", cluster_name
);
2024 free(*cluster_name
);
2033 dlclose(lib_handle
);