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 int parse_cluster_confirm_arg(char *input
, char **devname
, int *slot
)
286 *slot
= strtoul(input
, &dev
, 10);
287 if (dev
== input
|| dev
[0] != ':')
293 void remove_partitions(int fd
)
295 /* remove partitions from this block devices.
296 * This is used for components added to an array
298 #ifdef BLKPG_DEL_PARTITION
299 struct blkpg_ioctl_arg a
;
300 struct blkpg_partition p
;
302 a
.op
= BLKPG_DEL_PARTITION
;
304 a
.datalen
= sizeof(p
);
306 memset(a
.data
, 0, a
.datalen
);
307 for (p
.pno
= 0; p
.pno
< 16; p
.pno
++)
308 ioctl(fd
, BLKPG
, &a
);
312 int test_partition(int fd
)
314 /* Check if fd is a whole-disk or a partition.
315 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
316 * will return ENXIO on an invalid partition number.
318 struct blkpg_ioctl_arg a
;
319 struct blkpg_partition p
;
320 a
.op
= BLKPG_DEL_PARTITION
;
322 a
.datalen
= sizeof(p
);
324 memset(a
.data
, 0, a
.datalen
);
326 if (ioctl(fd
, BLKPG
, &a
) == 0)
327 /* Very unlikely, but not a partition */
329 if (errno
== ENXIO
|| errno
== ENOTTY
)
330 /* not a partition */
336 int test_partition_from_id(dev_t id
)
341 sprintf(buf
, "%d:%d", major(id
), minor(id
));
342 fd
= dev_open(buf
, O_RDONLY
);
345 rv
= test_partition(fd
);
350 int enough(int level
, int raid_disks
, int layout
, int clean
, char *avail
)
356 for (i
= 0; i
< raid_disks
; i
++)
357 avail_disks
+= !!avail
[i
];
361 /* This is the tricky one - we need to check
362 * which actual disks are present.
364 copies
= (layout
&255)* ((layout
>>8) & 255);
367 /* there must be one of the 'copies' form 'first' */
374 this = (this+1) % raid_disks
;
378 first
= (first
+(layout
&255)) % raid_disks
;
379 } while (first
!= 0);
382 case LEVEL_MULTIPATH
:
383 return avail_disks
>= 1;
386 return avail_disks
== raid_disks
;
388 return avail_disks
>= 1;
390 if (avail_disks
== raid_disks
- 1 &&
391 !avail
[raid_disks
- 1])
392 /* If just the parity device is missing, then we
393 * have enough, even if not clean
399 return avail_disks
>= raid_disks
-1;
401 return avail_disks
>= raid_disks
;
404 return avail_disks
>= raid_disks
-2;
406 return avail_disks
>= raid_disks
;
412 int enough_fd(int fd
)
414 struct mdu_array_info_s array
;
415 struct mdu_disk_info_s disk
;
419 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
420 array
.raid_disks
<= 0)
422 avail
= xcalloc(array
.raid_disks
, 1);
423 for (i
= 0; i
< MAX_DISKS
&& array
.nr_disks
> 0; i
++) {
425 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
427 if (disk
.major
== 0 && disk
.minor
== 0)
431 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
433 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
435 avail
[disk
.raid_disk
] = 1;
437 /* This is used on an active array, so assume it is clean */
438 rv
= enough(array
.level
, array
.raid_disks
, array
.layout
,
444 const int uuid_zero
[4] = { 0, 0, 0, 0 };
446 int same_uuid(int a
[4], int b
[4], int swapuuid
)
449 /* parse uuids are hostendian.
450 * uuid's from some superblocks are big-ending
451 * if there is a difference, we need to swap..
453 unsigned char *ac
= (unsigned char *)a
;
454 unsigned char *bc
= (unsigned char *)b
;
456 for (i
= 0; i
< 16; i
+= 4) {
457 if (ac
[i
+0] != bc
[i
+3] ||
458 ac
[i
+1] != bc
[i
+2] ||
459 ac
[i
+2] != bc
[i
+1] ||
474 void copy_uuid(void *a
, int b
[4], int swapuuid
)
477 /* parse uuids are hostendian.
478 * uuid's from some superblocks are big-ending
479 * if there is a difference, we need to swap..
481 unsigned char *ac
= (unsigned char *)a
;
482 unsigned char *bc
= (unsigned char *)b
;
484 for (i
= 0; i
< 16; i
+= 4) {
494 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
501 copy_uuid(uuid
, id
, swap
);
502 for (i
= 0; i
< 4; i
++) {
505 for (j
= 3; j
>= 0; j
--) {
506 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
514 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
516 // dirty hack to work around an issue with super1 superblocks...
517 // super1 superblocks need swapuuid set in order for assembly to
518 // work, but can't have it set if we want this printout to match
519 // all the other uuid printouts in super1.c, so we force swapuuid
520 // to 1 to make our printout match the rest of super1
521 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
525 int check_ext2(int fd
, char *name
)
528 * Check for an ext2fs file system.
529 * Superblock is always 1K at 1K offset
531 * s_magic is le16 at 56 == 0xEF53
532 * report mtime - le32 at 44
534 * logblksize - le32 at 24
536 unsigned char sb
[1024];
538 unsigned long long size
;
540 if (lseek(fd
, 1024,0)!= 1024)
542 if (read(fd
, sb
, 1024)!= 1024)
544 if (sb
[56] != 0x53 || sb
[57] != 0xef)
547 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
548 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
549 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
551 pr_err("%s appears to contain an ext2fs file system\n",
553 cont_err("size=%lluK mtime=%s", size
, ctime(&mtime
));
557 int check_reiser(int fd
, char *name
)
560 * superblock is at 64K
562 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
565 unsigned char sb
[1024];
566 unsigned long long size
;
567 if (lseek(fd
, 64*1024, 0) != 64*1024)
569 if (read(fd
, sb
, 1024) != 1024)
571 if (strncmp((char*)sb
+52, "ReIsErFs",8) != 0 &&
572 strncmp((char*)sb
+52, "ReIsEr2Fs",9) != 0)
574 pr_err("%s appears to contain a reiserfs file system\n",name
);
575 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
576 cont_err("size = %lluK\n", size
*4);
581 int check_raid(int fd
, char *name
)
586 struct supertype
*st
= guess_super(fd
);
590 st
->ss
->load_super(st
, fd
, name
);
591 /* Looks like a raid array .. */
592 pr_err("%s appears to be part of a raid array:\n",
594 st
->ss
->getinfo_super(st
, &info
, NULL
);
595 st
->ss
->free_super(st
);
596 crtime
= info
.array
.ctime
;
597 level
= map_num(pers
, info
.array
.level
);
598 if (!level
) level
= "-unknown-";
599 cont_err("level=%s devices=%d ctime=%s",
600 level
, info
.array
.raid_disks
, ctime(&crtime
));
608 for (i
= 0; i
< 5; i
++) {
610 fprintf(stderr
, "%s%s", mesg
, add
);
612 if (fgets(buf
, 100, stdin
)==NULL
)
614 if (buf
[0]=='y' || buf
[0]=='Y')
616 if (buf
[0]=='n' || buf
[0]=='N')
620 pr_err("assuming 'no'\n");
623 #endif /* MDASSEMBLE */
625 int is_standard(char *dev
, int *nump
)
627 /* tests if dev is a "standard" md dev name.
628 * i.e if the last component is "/dNN" or "/mdNN",
629 * where NN is a string of digits
630 * Returns 1 if a partitionable standard,
631 * -1 if non-partitonable,
632 * 0 if not a standard name.
634 char *d
= strrchr(dev
, '/');
639 if (strncmp(d
, "/d",2) == 0)
640 d
+= 2, type
= 1; /* /dev/md/dN{pM} */
641 else if (strncmp(d
, "/md_d", 5) == 0)
642 d
+= 5, type
= 1; /* /dev/md_dN{pM} */
643 else if (strncmp(d
, "/md", 3) == 0)
644 d
+= 3, type
= -1; /* /dev/mdN */
645 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3) == 0)
646 d
+= 1, type
= -1; /* /dev/md/N */
656 if (nump
) *nump
= num
;
661 unsigned long calc_csum(void *super
, int bytes
)
663 unsigned long long newcsum
= 0;
666 unsigned int *superc
= (unsigned int*) super
;
668 for(i
= 0; i
< bytes
/4; i
++)
669 newcsum
+= superc
[i
];
670 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
672 /* The in-kernel checksum calculation is always 16bit on
673 * the alpha, though it is 32 bit on i386...
674 * I wonder what it is elsewhere... (it uses an API in
675 * a way that it shouldn't).
677 csum
= (csum
& 0xffff) + (csum
>> 16);
678 csum
= (csum
& 0xffff) + (csum
>> 16);
684 char *human_size(long long bytes
)
688 /* We convert bytes to either centi-M{ega,ibi}bytes or
689 * centi-G{igi,ibi}bytes, with appropriate rounding,
690 * and then print 1/100th of those as a decimal.
691 * We allow upto 2048Megabytes before converting to
692 * gigabytes, as that shows more precision and isn't
693 * too large a number.
694 * Terabytes are not yet handled.
697 if (bytes
< 5000*1024)
699 else if (bytes
< 2*1024LL*1024LL*1024LL) {
700 long cMiB
= (bytes
* 200LL / (1LL<<20) + 1) / 2;
701 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
702 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
703 cMiB
/100 , cMiB
% 100,
706 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) / 2;
707 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
708 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
709 cGiB
/100 , cGiB
% 100,
715 char *human_size_brief(long long bytes
, int prefix
)
719 /* We convert bytes to either centi-M{ega,ibi}bytes or
720 * centi-G{igi,ibi}bytes, with appropriate rounding,
721 * and then print 1/100th of those as a decimal.
722 * We allow upto 2048Megabytes before converting to
723 * gigabytes, as that shows more precision and isn't
724 * too large a number.
725 * Terabytes are not yet handled.
727 * If prefix == IEC, we mean prefixes like kibi,mebi,gibi etc.
728 * If prefix == JEDEC, we mean prefixes like kilo,mega,giga etc.
731 if (bytes
< 5000*1024)
733 else if (prefix
== IEC
) {
734 if (bytes
< 2*1024LL*1024LL*1024LL) {
735 long cMiB
= (bytes
* 200LL / (1LL<<20) +1) /2;
736 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
737 cMiB
/100 , cMiB
% 100);
739 long cGiB
= (bytes
* 200LL / (1LL<<30) +1) /2;
740 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
741 cGiB
/100 , cGiB
% 100);
744 else if (prefix
== JEDEC
) {
745 if (bytes
< 2*1024LL*1024LL*1024LL) {
746 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
747 snprintf(buf
, sizeof(buf
), "%ld.%02ldMB",
750 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
751 snprintf(buf
, sizeof(buf
), "%ld.%02ldGB",
752 cGB
/100 , cGB
% 100);
761 void print_r10_layout(int layout
)
763 int near
= layout
& 255;
764 int far
= (layout
>> 8) & 255;
765 int offset
= (layout
&0x10000);
769 printf("%s near=%d", sep
, near
);
773 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
775 printf("NO REDUNDANCY");
779 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
780 int chunksize
, unsigned long long devsize
)
784 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
785 return get_data_disks(level
, layout
, raid_disks
) * devsize
;
788 int get_data_disks(int level
, int layout
, int raid_disks
)
792 case 0: data_disks
= raid_disks
;
794 case 1: data_disks
= 1;
797 case 5: data_disks
= raid_disks
- 1;
799 case 6: data_disks
= raid_disks
- 2;
801 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
808 int devnm2devid(char *devnm
)
810 /* First look in /sys/block/$DEVNM/dev for %d:%d
811 * If that fails, try parsing out a number
818 sprintf(path
, "/sys/block/%s/dev", devnm
);
819 fd
= open(path
, O_RDONLY
);
822 int n
= read(fd
, buf
, sizeof(buf
));
826 if (n
> 0 && sscanf(buf
, "%d:%d\n", &mjr
, &mnr
) == 2)
827 return makedev(mjr
, mnr
);
829 if (strncmp(devnm
, "md_d", 4) == 0 &&
831 (mnr
= strtoul(devnm
+4, &ep
, 10)) >= 0 &&
832 ep
> devnm
&& *ep
== 0)
833 return makedev(get_mdp_major(), mnr
<< MdpMinorShift
);
835 if (strncmp(devnm
, "md", 2) == 0 &&
837 (mnr
= strtoul(devnm
+2, &ep
, 10)) >= 0 &&
838 ep
> devnm
&& *ep
== 0)
839 return makedev(MD_MAJOR
, mnr
);
844 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
845 char *get_md_name(char *devnm
)
847 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
848 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
850 static char devname
[50];
852 dev_t rdev
= devnm2devid(devnm
);
857 if (strncmp(devnm
, "md_", 3) == 0) {
858 snprintf(devname
, sizeof(devname
), "/dev/md/%s",
860 if (stat(devname
, &stb
) == 0
861 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
862 && (stb
.st_rdev
== rdev
))
865 snprintf(devname
, sizeof(devname
), "/dev/%s", devnm
);
866 if (stat(devname
, &stb
) == 0
867 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
868 && (stb
.st_rdev
== rdev
))
871 snprintf(devname
, sizeof(devname
), "/dev/md/%s", devnm
+2);
872 if (stat(devname
, &stb
) == 0
873 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
874 && (stb
.st_rdev
== rdev
))
877 dn
= map_dev(major(rdev
), minor(rdev
), 0);
880 snprintf(devname
, sizeof(devname
), "/dev/.tmp.%s", devnm
);
881 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
885 if (stat(devname
, &stb
) == 0
886 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
887 && (stb
.st_rdev
== rdev
))
893 void put_md_name(char *name
)
895 if (strncmp(name
, "/dev/.tmp.md", 12) == 0)
898 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
900 int get_maj_min(char *dev
, int *major
, int *minor
)
903 *major
= strtoul(dev
, &e
, 0);
904 return (e
> dev
&& *e
== ':' && e
[1] &&
905 (*minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
909 int dev_open(char *dev
, int flags
)
911 /* like 'open', but if 'dev' matches %d:%d, create a temp
912 * block device and open that
922 if (get_maj_min(dev
, &major
, &minor
)) {
923 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
924 (int)getpid(), major
, minor
);
925 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
926 fd
= open(devname
, flags
);
930 /* Try /tmp as /dev appear to be read-only */
931 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
932 (int)getpid(), major
, minor
);
933 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
)) == 0) {
934 fd
= open(devname
, flags
);
939 fd
= open(dev
, flags
);
943 int open_dev_flags(char *devnm
, int flags
)
948 devid
= devnm2devid(devnm
);
949 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
950 return dev_open(buf
, flags
);
953 int open_dev(char *devnm
)
955 return open_dev_flags(devnm
, O_RDONLY
);
958 int open_dev_excl(char *devnm
)
963 int devid
= devnm2devid(devnm
);
966 sprintf(buf
, "%d:%d", major(devid
), minor(devid
));
967 for (i
= 0 ; i
< 25 ; i
++) {
968 int fd
= dev_open(buf
, flags
|O_EXCL
);
971 if (errno
== EACCES
&& flags
== O_RDWR
) {
984 int same_dev(char *one
, char *two
)
986 struct stat st1
, st2
;
987 if (stat(one
, &st1
) != 0)
989 if (stat(two
, &st2
) != 0)
991 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
993 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
995 return st1
.st_rdev
== st2
.st_rdev
;
998 void wait_for(char *dev
, int fd
)
1001 struct stat stb_want
;
1004 if (fstat(fd
, &stb_want
) != 0 ||
1005 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
1008 for (i
= 0 ; i
< 25 ; i
++) {
1010 if (stat(dev
, &stb
) == 0 &&
1011 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1012 (stb
.st_rdev
== stb_want
.st_rdev
))
1019 dprintf("timeout waiting for %s\n", dev
);
1022 struct superswitch
*superlist
[] =
1025 &super_ddf
, &super_imsm
,
1029 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1031 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
1033 mdu_array_info_t array
;
1036 struct supertype
*st
= NULL
;
1041 char *subarray
= NULL
;
1042 char container
[32] = "";
1044 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
1047 vers
= sra
->array
.major_version
;
1048 minor
= sra
->array
.minor_version
;
1049 verstr
= sra
->text_version
;
1051 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1052 array
.major_version
= array
.minor_version
= 0;
1053 vers
= array
.major_version
;
1054 minor
= array
.minor_version
;
1059 sprintf(version
, "%d.%d", vers
, minor
);
1062 if (minor
== -2 && is_subarray(verstr
)) {
1063 char *dev
= verstr
+1;
1065 subarray
= strchr(dev
, '/');
1068 subarray
= xstrdup(subarray
);
1070 strcpy(container
, dev
);
1073 sra
= sysfs_read(-1, container
, GET_VERSION
);
1074 if (sra
&& sra
->text_version
[0])
1075 verstr
= sra
->text_version
;
1077 verstr
= "-no-metadata-";
1080 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1081 st
= superlist
[i
]->match_metadata_desc(verstr
);
1088 *subarrayp
= subarray
;
1089 strcpy(st
->container_devnm
, container
);
1090 strcpy(st
->devnm
, fd2devnm(fd
));
1096 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1098 int dev_size_from_id(dev_t id
, unsigned long long *size
)
1103 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1104 fd
= dev_open(buf
, O_RDONLY
);
1107 if (get_dev_size(fd
, NULL
, size
)) {
1115 struct supertype
*dup_super(struct supertype
*orig
)
1117 struct supertype
*st
;
1121 st
= xcalloc(1, sizeof(*st
));
1123 st
->max_devs
= orig
->max_devs
;
1124 st
->minor_version
= orig
->minor_version
;
1125 st
->ignore_hw_compat
= orig
->ignore_hw_compat
;
1126 st
->data_offset
= orig
->data_offset
;
1132 struct supertype
*guess_super_type(int fd
, enum guess_types guess_type
)
1134 /* try each load_super to find the best match,
1135 * and return the best superswitch
1137 struct superswitch
*ss
;
1138 struct supertype
*st
;
1139 time_t besttime
= 0;
1143 st
= xcalloc(1, sizeof(*st
));
1144 st
->container_devnm
[0] = 0;
1146 for (i
= 0 ; superlist
[i
]; i
++) {
1149 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1151 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1153 memset(st
, 0, sizeof(*st
));
1154 st
->ignore_hw_compat
= 1;
1155 rv
= ss
->load_super(st
, fd
, NULL
);
1158 st
->ss
->getinfo_super(st
, &info
, NULL
);
1159 if (bestsuper
== -1 ||
1160 besttime
< info
.array
.ctime
) {
1162 besttime
= info
.array
.ctime
;
1167 if (bestsuper
!= -1) {
1169 memset(st
, 0, sizeof(*st
));
1170 st
->ignore_hw_compat
= 1;
1171 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1173 superlist
[bestsuper
]->free_super(st
);
1181 /* Return size of device in bytes */
1182 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1184 unsigned long long ldsize
;
1187 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1188 ldsize
= (unsigned long long)st
.st_size
;
1191 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1194 unsigned long dsize
;
1195 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1200 pr_err("Cannot get size of %s: %s\b",
1201 dname
, strerror(errno
));
1209 /* Return true if this can only be a container, not a member device.
1210 * i.e. is and md device and size is zero
1212 int must_be_container(int fd
)
1214 unsigned long long size
;
1215 if (md_get_version(fd
) < 0)
1217 if (get_dev_size(fd
, NULL
, &size
) == 0)
1224 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1225 * Returns: 1 if successful
1226 * -1 for unknown partition type
1227 * 0 for other errors
1229 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1232 unsigned char empty_gpt_entry
[16]= {0};
1233 struct GPT_part_entry
*part
;
1235 unsigned long long curr_part_end
;
1236 unsigned all_partitions
, entry_size
;
1241 BUILD_BUG_ON(sizeof(gpt
) != 512);
1242 /* skip protective MBR */
1243 lseek(fd
, 512, SEEK_SET
);
1244 /* read GPT header */
1245 if (read(fd
, &gpt
, 512) != 512)
1248 /* get the number of partition entries and the entry size */
1249 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1250 entry_size
= __le32_to_cpu(gpt
.part_size
);
1252 /* Check GPT signature*/
1253 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1257 if (all_partitions
> 1024 ||
1258 entry_size
> sizeof(buf
))
1261 part
= (struct GPT_part_entry
*)buf
;
1263 for (part_nr
= 0; part_nr
< all_partitions
; part_nr
++) {
1264 /* read partition entry */
1265 if (read(fd
, buf
, entry_size
) != (ssize_t
)entry_size
)
1268 /* is this valid partition? */
1269 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1270 /* check the last lba for the current partition */
1271 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1272 if (curr_part_end
> *endofpart
)
1273 *endofpart
= curr_part_end
;
1280 /* Sets endofpart parameter to the last block used by the last partition on the device.
1281 * Returns: 1 if successful
1282 * -1 for unknown partition type
1283 * 0 for other errors
1285 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1287 struct MBR boot_sect
;
1288 struct MBR_part_record
*part
;
1289 unsigned long long curr_part_end
;
1295 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1298 if (read(fd
, &boot_sect
, 512) != 512)
1301 /* check MBP signature */
1302 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1304 /* found the correct signature */
1305 part
= boot_sect
.parts
;
1307 for (part_nr
= 0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1308 /* check for GPT type */
1309 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1310 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1313 /* check the last used lba for the current partition */
1314 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1315 __le32_to_cpu(part
->blocks_num
);
1316 if (curr_part_end
> *endofpart
)
1317 *endofpart
= curr_part_end
;
1322 /* Unknown partition table */
1329 int check_partitions(int fd
, char *dname
, unsigned long long freesize
,
1330 unsigned long long size
)
1333 * Check where the last partition ends
1335 unsigned long long endofpart
;
1338 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1339 /* There appears to be a partition table here */
1340 if (freesize
== 0) {
1341 /* partitions will not be visible in new device */
1342 pr_err("partition table exists on %s but will be lost or\n"
1343 " meaningless after creating array\n",
1346 } else if (endofpart
> freesize
) {
1347 /* last partition overlaps metadata */
1348 pr_err("metadata will over-write last partition on %s.\n",
1351 } else if (size
&& endofpart
> size
) {
1352 /* partitions will be truncated in new device */
1353 pr_err("array size is too small to cover all partitions on %s.\n",
1361 int open_container(int fd
)
1363 /* 'fd' is a block device. Find out if it is in use
1364 * by a container, and return an open fd on that container.
1375 if (fstat(fd
, &st
) != 0)
1377 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1378 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1379 e
= path
+ strlen(path
);
1381 dir
= opendir(path
);
1384 while ((de
= readdir(dir
))) {
1387 if (de
->d_name
[0] == '.')
1389 /* Need to make sure it is a container and not a volume */
1390 sprintf(e
, "/%s/md/metadata_version", de
->d_name
);
1391 dfd
= open(path
, O_RDONLY
);
1394 n
= read(dfd
, buf
, sizeof(buf
));
1396 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1399 if (strncmp(buf
, "external", 8) != 0 ||
1403 sprintf(e
, "/%s/dev", de
->d_name
);
1404 dfd
= open(path
, O_RDONLY
);
1407 n
= read(dfd
, buf
, sizeof(buf
));
1409 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1412 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1414 sprintf(buf
, "%d:%d", major
, minor
);
1415 dfd
= dev_open(buf
, O_RDONLY
);
1425 struct superswitch
*version_to_superswitch(char *vers
)
1429 for (i
= 0; superlist
[i
]; i
++) {
1430 struct superswitch
*ss
= superlist
[i
];
1432 if (strcmp(vers
, ss
->name
) == 0)
1439 int metadata_container_matches(char *metadata
, char *devnm
)
1441 /* Check if 'devnm' is the container named in 'metadata'
1443 * /containername/componentname or
1444 * -containername/componentname
1447 if (*metadata
!= '/' && *metadata
!= '-')
1450 if (strncmp(metadata
+1, devnm
, l
) != 0)
1452 if (metadata
[l
+1] != '/')
1457 int metadata_subdev_matches(char *metadata
, char *devnm
)
1459 /* Check if 'devnm' is the subdev named in 'metadata'
1461 * /containername/subdev or
1462 * -containername/subdev
1465 if (*metadata
!= '/' && *metadata
!= '-')
1467 sl
= strchr(metadata
+1, '/');
1470 if (strcmp(sl
+1, devnm
) == 0)
1475 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1477 if (mdstat
->metadata_version
== NULL
||
1478 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1479 !metadata_container_matches(mdstat
->metadata_version
+9, container
))
1485 int is_subarray_active(char *subarray
, char *container
)
1487 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1488 struct mdstat_ent
*ent
;
1490 for (ent
= mdstat
; ent
; ent
= ent
->next
)
1491 if (is_container_member(ent
, container
))
1492 if (strcmp(to_subarray(ent
, container
), subarray
) == 0)
1495 free_mdstat(mdstat
);
1500 /* open_subarray - opens a subarray in a container
1501 * @dev: container device name
1502 * @st: empty supertype
1503 * @quiet: block reporting errors flag
1505 * On success returns an fd to a container and fills in *st
1507 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1510 struct mdinfo
*info
;
1514 fd
= open(dev
, O_RDWR
|O_EXCL
);
1517 pr_err("Couldn't open %s, aborting\n",
1522 _devnm
= fd2devnm(fd
);
1523 if (_devnm
== NULL
) {
1525 pr_err("Failed to determine device number for %s\n",
1529 strcpy(st
->devnm
, _devnm
);
1531 mdi
= sysfs_read(fd
, st
->devnm
, GET_VERSION
|GET_LEVEL
);
1534 pr_err("Failed to read sysfs for %s\n",
1539 if (mdi
->array
.level
!= UnSet
) {
1541 pr_err("%s is not a container\n", dev
);
1545 st
->ss
= version_to_superswitch(mdi
->text_version
);
1548 pr_err("Operation not supported for %s metadata\n",
1553 if (st
->devnm
[0] == 0) {
1555 pr_err("Failed to allocate device name\n");
1559 if (!st
->ss
->load_container
) {
1561 pr_err("%s is not a container\n", dev
);
1565 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1567 pr_err("Failed to load metadata for %s\n",
1572 info
= st
->ss
->container_content(st
, subarray
);
1575 pr_err("Failed to find subarray-%s in %s\n",
1585 st
->ss
->free_super(st
);
1598 int add_disk(int mdfd
, struct supertype
*st
,
1599 struct mdinfo
*sra
, struct mdinfo
*info
)
1601 /* Add a device to an array, in one of 2 ways. */
1604 if (st
->ss
->external
) {
1605 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1606 info
->recovery_start
= MaxSector
;
1608 info
->recovery_start
= 0;
1609 rv
= sysfs_add_disk(sra
, info
, 0);
1612 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1616 sd2
= xmalloc(sizeof(*sd2
));
1618 sd2
->next
= sra
->devs
;
1624 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1628 int remove_disk(int mdfd
, struct supertype
*st
,
1629 struct mdinfo
*sra
, struct mdinfo
*info
)
1632 /* Remove the disk given by 'info' from the array */
1634 if (st
->ss
->external
)
1635 rv
= sysfs_set_str(sra
, info
, "slot", "none");
1638 rv
= ioctl(mdfd
, HOT_REMOVE_DISK
, makedev(info
->disk
.major
,
1643 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1645 /* Initialise kernel's knowledge of array.
1646 * This varies between externally managed arrays
1649 int vers
= md_get_version(mdfd
);
1653 if (st
->ss
->external
)
1654 rv
= sysfs_set_array(info
, vers
);
1657 if ((vers
% 100) >= 1) { /* can use different versions */
1658 mdu_array_info_t inf
;
1659 memset(&inf
, 0, sizeof(inf
));
1660 inf
.major_version
= info
->array
.major_version
;
1661 inf
.minor_version
= info
->array
.minor_version
;
1662 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1664 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1668 unsigned long long min_recovery_start(struct mdinfo
*array
)
1670 /* find the minimum recovery_start in an array for metadata
1671 * formats that only record per-array recovery progress instead
1674 unsigned long long recovery_start
= MaxSector
;
1677 for (d
= array
->devs
; d
; d
= d
->next
)
1678 recovery_start
= min(recovery_start
, d
->recovery_start
);
1680 return recovery_start
;
1683 int mdmon_pid(char *devnm
)
1690 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devnm
);
1692 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1696 n
= read(fd
, pid
, 9);
1703 int mdmon_running(char *devnm
)
1705 int pid
= mdmon_pid(devnm
);
1708 if (kill(pid
, 0) == 0)
1713 int start_mdmon(char *devnm
)
1727 if (check_env("MDADM_NO_MDMON"))
1730 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
)-1);
1734 sl
= strrchr(pathbuf
, '/');
1739 strcpy(sl
, "mdmon");
1743 /* First try to run systemctl */
1744 if (!check_env("MDADM_NO_SYSTEMCTL"))
1747 /* FIXME yuk. CLOSE_EXEC?? */
1749 for (i
= 3; skipped
< 20; i
++)
1755 /* Don't want to see error messages from
1756 * systemctl. If the service doesn't exist,
1757 * we start mdmon ourselves.
1760 open("/dev/null", O_WRONLY
);
1761 snprintf(pathbuf
, sizeof(pathbuf
), "mdmon@%s.service",
1763 status
= execl("/usr/bin/systemctl", "systemctl",
1766 status
= execl("/bin/systemctl", "systemctl", "start",
1769 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
1771 default: /* parent - good */
1772 pid
= wait(&status
);
1773 if (pid
>= 0 && status
== 0)
1777 /* That failed, try running mdmon directly */
1780 /* FIXME yuk. CLOSE_EXEC?? */
1782 for (i
= 3; skipped
< 20; i
++)
1788 for (i
= 0; paths
[i
]; i
++)
1790 execl(paths
[i
], paths
[i
],
1794 case -1: pr_err("cannot run mdmon. Array remains readonly\n");
1796 default: /* parent - good */
1797 pid
= wait(&status
);
1798 if (pid
< 0 || status
!= 0) {
1799 pr_err("failed to launch mdmon. Array remains readonly\n");
1806 __u32
random32(void)
1809 int rfd
= open("/dev/urandom", O_RDONLY
);
1810 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1818 int flush_metadata_updates(struct supertype
*st
)
1822 st
->update_tail
= NULL
;
1826 sfd
= connect_monitor(st
->container_devnm
);
1830 while (st
->updates
) {
1831 struct metadata_update
*mu
= st
->updates
;
1832 st
->updates
= mu
->next
;
1834 send_message(sfd
, mu
, 0);
1842 st
->update_tail
= NULL
;
1846 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1849 struct metadata_update
*mu
= xmalloc(sizeof(*mu
));
1854 mu
->space_list
= NULL
;
1856 *st
->update_tail
= mu
;
1857 st
->update_tail
= &mu
->next
;
1859 #endif /* MDASSEMBLE */
1862 /* tinyc doesn't optimize this check in ioctl.h out ... */
1863 unsigned int __invalid_size_argument_for_IOC
= 0;
1866 int experimental(void)
1868 if (check_env("MDADM_EXPERIMENTAL"))
1871 pr_err("To use this feature MDADM_EXPERIMENTAL environment variable has to be defined.\n");
1876 /* Pick all spares matching given criteria from a container
1877 * if min_size == 0 do not check size
1878 * if domlist == NULL do not check domains
1879 * if spare_group given add it to domains of each spare
1880 * metadata allows to test domains using metadata of destination array */
1881 struct mdinfo
*container_choose_spares(struct supertype
*st
,
1882 unsigned long long min_size
,
1883 struct domainlist
*domlist
,
1885 const char *metadata
, int get_one
)
1887 struct mdinfo
*d
, **dp
, *disks
= NULL
;
1889 /* get list of all disks in container */
1890 if (st
->ss
->getinfo_super_disks
)
1891 disks
= st
->ss
->getinfo_super_disks(st
);
1895 /* find spare devices on the list */
1897 disks
->array
.spare_disks
= 0;
1901 if (d
->disk
.state
== 0) {
1902 /* check if size is acceptable */
1903 unsigned long long dev_size
;
1904 dev_t dev
= makedev(d
->disk
.major
,d
->disk
.minor
);
1907 (dev_size_from_id(dev
, &dev_size
) &&
1908 dev_size
>= min_size
))
1910 /* check if domain matches */
1911 if (found
&& domlist
) {
1912 struct dev_policy
*pol
= devid_policy(dev
);
1914 pol_add(&pol
, pol_domain
,
1916 if (domain_test(domlist
, pol
, metadata
) != 1)
1918 dev_policy_free(pol
);
1923 disks
->array
.spare_disks
++;
1937 /* Checks if paths point to the same device
1938 * Returns 0 if they do.
1939 * Returns 1 if they don't.
1940 * Returns -1 if something went wrong,
1941 * e.g. paths are empty or the files
1942 * they point to don't exist */
1943 int compare_paths (char* path1
, char* path2
)
1945 struct stat st1
,st2
;
1947 if (path1
== NULL
|| path2
== NULL
)
1949 if (stat(path1
,&st1
) != 0)
1951 if (stat(path2
,&st2
) != 0)
1953 if ((st1
.st_ino
== st2
.st_ino
) && (st1
.st_dev
== st2
.st_dev
))
1958 /* Make sure we can open as many devices as needed */
1959 void enable_fds(int devices
)
1961 unsigned int fds
= 20 + devices
;
1963 if (getrlimit(RLIMIT_NOFILE
, &lim
) != 0
1964 || lim
.rlim_cur
>= fds
)
1966 if (lim
.rlim_max
< fds
)
1969 setrlimit(RLIMIT_NOFILE
, &lim
);
1974 /* This is based on similar function in systemd. */
1976 /* statfs.f_type is signed long on s390x and MIPS, causing all
1977 sorts of sign extension problems with RAMFS_MAGIC being
1978 defined as 0x858458f6 */
1979 return statfs("/", &s
) >= 0 &&
1980 ((unsigned long)s
.f_type
== TMPFS_MAGIC
||
1981 ((unsigned long)s
.f_type
& 0xFFFFFFFFUL
) ==
1982 ((unsigned long)RAMFS_MAGIC
& 0xFFFFFFFFUL
));
1985 void reopen_mddev(int mdfd
)
1987 /* Re-open without any O_EXCL, but keep
1992 devnm
= fd2devnm(mdfd
);
1994 fd
= open_dev(devnm
);
1995 if (fd
>= 0 && fd
!= mdfd
)
1999 int get_cluster_name(char **cluster_name
)
2001 void *lib_handle
= NULL
;
2004 cmap_handle_t handle
;
2005 static int (*initialize
)(cmap_handle_t
*handle
);
2006 static int (*get_string
)(cmap_handle_t handle
,
2009 static int (*finalize
)(cmap_handle_t handle
);
2012 lib_handle
= dlopen("libcmap.so.4", RTLD_NOW
| RTLD_LOCAL
);
2016 initialize
= dlsym(lib_handle
, "cmap_initialize");
2020 get_string
= dlsym(lib_handle
, "cmap_get_string");
2024 finalize
= dlsym(lib_handle
, "cmap_finalize");
2028 rv
= initialize(&handle
);
2032 rv
= get_string(handle
, "totem.cluster_name", cluster_name
);
2034 free(*cluster_name
);
2043 dlclose(lib_handle
);