2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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>
36 * following taken from linux/blkpg.h because they aren't
37 * anywhere else and it isn't safe to #include linux/ * stuff.
40 #define BLKPG _IO(0x12,105)
42 /* The argument structure */
43 struct blkpg_ioctl_arg
{
50 /* The subfunctions (for the op field) */
51 #define BLKPG_ADD_PARTITION 1
52 #define BLKPG_DEL_PARTITION 2
54 /* Sizes of name fields. Unused at present. */
55 #define BLKPG_DEVNAMELTH 64
56 #define BLKPG_VOLNAMELTH 64
58 /* The data structure for ADD_PARTITION and DEL_PARTITION */
59 struct blkpg_partition
{
60 long long start
; /* starting offset in bytes */
61 long long length
; /* length in bytes */
62 int pno
; /* partition number */
63 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
64 to be used in kernel messages */
65 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
68 /* partition table structures so we can check metadata position
69 * against the end of the last partition.
70 * Only handle MBR ant GPT partition tables.
72 struct MBR_part_record
{
87 struct MBR_part_record parts
[4];
89 } __attribute__((packed
));
91 struct GPT_part_entry
{
92 unsigned char type_guid
[16];
93 unsigned char partition_guid
[16];
96 unsigned char attr_bits
[8];
97 unsigned char name
[72];
98 } __attribute__((packed
));
116 } __attribute__((packed
));
118 /* Force a compilation error if condition is true */
119 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
121 /* Force a compilation error if condition is true, but also produce a
122 result (of value 0 and type size_t), so the expression can be used
123 e.g. in a structure initializer (or where-ever else comma expressions
124 aren't permitted). */
125 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
128 /* MBR/GPT magic numbers */
129 #define MBR_SIGNATURE_MAGIC __cpu_to_le16(0xAA55)
130 #define GPT_SIGNATURE_MAGIC __cpu_to_le64(0x5452415020494645ULL)
132 #define MBR_PARTITIONS 4
133 #define MBR_GPT_PARTITION_TYPE 0xEE
136 * Parse a 128 bit uuid in 4 integers
137 * format is 32 hexx nibbles with options :.<space> separator
138 * If not exactly 32 hex digits are found, return 0
141 int parse_uuid(char *str
, int uuid
[4])
143 int hit
= 0; /* number of Hex digIT */
146 for (i
=0; i
<4; i
++) uuid
[i
]=0;
148 while ((c
= *str
++)) {
150 if (c
>='0' && c
<='9')
152 else if (c
>='a' && c
<= 'f')
154 else if (c
>='A' && c
<= 'F')
156 else if (strchr(":. -", c
))
173 * Get the md version number.
174 * We use the RAID_VERSION ioctl if it is supported
175 * If not, but we have a block device with major '9', we assume
178 * Return version number as 24 but number - assume version parts
182 int md_get_version(int fd
)
187 if (fstat(fd
, &stb
)<0)
189 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
192 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
193 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
196 if (major(stb
.st_rdev
) == MD_MAJOR
)
201 int get_linux_version()
210 a
= strtoul(cp
, &cp
, 10);
211 if (*cp
!= '.') return -1;
212 b
= strtoul(cp
+1, &cp
, 10);
213 if (*cp
!= '.') return -1;
214 c
= strtoul(cp
+1, NULL
, 10);
216 return (a
*1000000)+(b
*1000)+c
;
220 long long parse_size(char *size
)
222 /* parse 'size' which should be a number optionally
223 * followed by 'K', 'M', or 'G'.
224 * Without a suffix, K is assumed.
225 * Number returned is in sectors (half-K)
228 long long s
= strtoll(size
, &c
, 10);
242 s
*= 1024 * 1024 * 2;
251 int parse_layout_10(char *layout
)
255 /* Parse the layout string for raid10 */
256 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
257 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
258 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
262 if (layout
[0] == 'n')
264 else if (layout
[0] == 'o')
265 rv
= 0x10000 + (copies
<<8) + 1;
267 rv
= 1 + (copies
<<8);
271 int parse_layout_faulty(char *layout
)
273 /* Parse the layout string for 'faulty' */
274 int ln
= strcspn(layout
, "0123456789");
275 char *m
= strdup(layout
);
278 mode
= map_name(faultylayout
, m
);
282 return mode
| (atoi(layout
+ln
)<< ModeShift
);
286 void remove_partitions(int fd
)
288 /* remove partitions from this block devices.
289 * This is used for components added to an array
291 #ifdef BLKPG_DEL_PARTITION
292 struct blkpg_ioctl_arg a
;
293 struct blkpg_partition p
;
295 a
.op
= BLKPG_DEL_PARTITION
;
297 a
.datalen
= sizeof(p
);
299 memset(a
.data
, 0, a
.datalen
);
300 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
301 ioctl(fd
, BLKPG
, &a
);
305 int enough(int level
, int raid_disks
, int layout
, int clean
,
306 char *avail
, int avail_disks
)
311 /* This is the tricky one - we need to check
312 * which actual disks are present.
314 copies
= (layout
&255)* ((layout
>>8) & 255);
317 /* there must be one of the 'copies' form 'first' */
323 first
= (first
+1) % raid_disks
;
328 } while (first
!= 0);
331 case LEVEL_MULTIPATH
:
332 return avail_disks
>= 1;
335 return avail_disks
== raid_disks
;
337 return avail_disks
>= 1;
341 return avail_disks
>= raid_disks
-1;
343 return avail_disks
>= raid_disks
;
346 return avail_disks
>= raid_disks
-2;
348 return avail_disks
>= raid_disks
;
354 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
355 int same_uuid(int a
[4], int b
[4], int swapuuid
)
357 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
358 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
362 /* parse uuids are hostendian.
363 * uuid's from some superblocks are big-ending
364 * if there is a difference, we need to swap..
366 unsigned char *ac
= (unsigned char *)a
;
367 unsigned char *bc
= (unsigned char *)b
;
369 for (i
=0; i
<16; i
+= 4) {
370 if (ac
[i
+0] != bc
[i
+3] ||
371 ac
[i
+1] != bc
[i
+2] ||
372 ac
[i
+2] != bc
[i
+1] ||
386 void copy_uuid(void *a
, int b
[4], int swapuuid
)
389 /* parse uuids are hostendian.
390 * uuid's from some superblocks are big-ending
391 * if there is a difference, we need to swap..
393 unsigned char *ac
= (unsigned char *)a
;
394 unsigned char *bc
= (unsigned char *)b
;
396 for (i
=0; i
<16; i
+= 4) {
406 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
413 copy_uuid(uuid
, id
, swap
);
414 for (i
= 0; i
< 4; i
++) {
417 for (j
= 3; j
>= 0; j
--) {
418 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
426 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
428 return __fname_from_uuid(info
->uuid
, st
->ss
->swapuuid
, buf
, sep
);
432 int check_ext2(int fd
, char *name
)
435 * Check for an ext2fs file system.
436 * Superblock is always 1K at 1K offset
438 * s_magic is le16 at 56 == 0xEF53
439 * report mtime - le32 at 44
441 * logblksize - le32 at 24
443 unsigned char sb
[1024];
446 if (lseek(fd
, 1024,0)!= 1024)
448 if (read(fd
, sb
, 1024)!= 1024)
450 if (sb
[56] != 0x53 || sb
[57] != 0xef)
453 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
454 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
455 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
456 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
458 fprintf(stderr
," size=%dK mtime=%s",
459 size
*(1<<bsize
), ctime(&mtime
));
463 int check_reiser(int fd
, char *name
)
466 * superblock is at 64K
468 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
471 unsigned char sb
[1024];
473 if (lseek(fd
, 64*1024, 0) != 64*1024)
475 if (read(fd
, sb
, 1024) != 1024)
477 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
478 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
480 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
481 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
482 fprintf(stderr
, " size = %luK\n", size
*4);
487 int check_raid(int fd
, char *name
)
492 struct supertype
*st
= guess_super(fd
);
495 st
->ss
->load_super(st
, fd
, name
);
496 /* Looks like a raid array .. */
497 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
499 st
->ss
->getinfo_super(st
, &info
);
500 st
->ss
->free_super(st
);
501 crtime
= info
.array
.ctime
;
502 level
= map_num(pers
, info
.array
.level
);
503 if (!level
) level
= "-unknown-";
504 fprintf(stderr
, " level=%s devices=%d ctime=%s",
505 level
, info
.array
.raid_disks
, ctime(&crtime
));
513 for (i
=0; i
<5; i
++) {
515 fprintf(stderr
, "%s%s", mesg
, add
);
517 if (fgets(buf
, 100, stdin
)==NULL
)
519 if (buf
[0]=='y' || buf
[0]=='Y')
521 if (buf
[0]=='n' || buf
[0]=='N')
525 fprintf(stderr
, Name
": assuming 'no'\n");
528 #endif /* MDASSEMBLE */
530 char *map_num(mapping_t
*map
, int num
)
540 int map_name(mapping_t
*map
, char *name
)
543 if (strcmp(map
->name
, name
)==0)
551 int is_standard(char *dev
, int *nump
)
553 /* tests if dev is a "standard" md dev name.
554 * i.e if the last component is "/dNN" or "/mdNN",
555 * where NN is a string of digits
556 * Returns 1 if a partitionable standard,
557 * -1 if non-partitonable,
558 * 0 if not a standard name.
560 char *d
= strrchr(dev
, '/');
565 if (strncmp(d
, "/d",2)==0)
566 d
+= 2, type
=1; /* /dev/md/dN{pM} */
567 else if (strncmp(d
, "/md_d", 5)==0)
568 d
+= 5, type
=1; /* /dev/md_dN{pM} */
569 else if (strncmp(d
, "/md", 3)==0)
570 d
+= 3, type
=-1; /* /dev/mdN */
571 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
572 d
+= 1, type
=-1; /* /dev/md/N */
582 if (nump
) *nump
= num
;
589 * convert a major/minor pair for a block device into a name in /dev, if possible.
590 * On the first call, walk /dev collecting name.
591 * Put them in a simple linked listfor now.
598 int devlist_ready
= 0;
600 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
604 if (S_ISLNK(stb
->st_mode
)) {
605 if (stat(name
, &st
) != 0)
610 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
611 char *n
= strdup(name
);
612 struct devmap
*dm
= malloc(sizeof(*dm
));
613 if (strncmp(n
, "/dev/./", 7)==0)
616 dm
->major
= major(stb
->st_rdev
);
617 dm
->minor
= minor(stb
->st_rdev
);
628 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
630 return add_dev(name
, stb
, flag
, NULL
);
632 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
634 return ftw(path
, add_dev_1
, nopenfd
);
637 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
641 #endif /* HAVE_FTW */
642 #endif /* HAVE_NFTW */
645 * Find a block device with the right major/minor number.
646 * If we find multiple names, choose the shortest.
647 * If we find a name in /dev/md/, we prefer that.
648 * This applies only to names for MD devices.
650 char *map_dev(int major
, int minor
, int create
)
653 char *regular
= NULL
, *preferred
=NULL
;
656 if (major
== 0 && minor
== 0)
660 if (!devlist_ready
) {
664 struct devmap
*d
= devlist
;
669 if (lstat(dev
, &stb
)==0 &&
670 S_ISLNK(stb
.st_mode
))
672 nftw(dev
, add_dev
, 10, FTW_PHYS
);
677 for (p
=devlist
; p
; p
=p
->next
)
678 if (p
->major
== major
&&
680 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
681 if (preferred
== NULL
||
682 strlen(p
->name
) < strlen(preferred
))
685 if (regular
== NULL
||
686 strlen(p
->name
) < strlen(regular
))
690 if (!regular
&& !preferred
&& !did_check
) {
694 if (create
&& !regular
&& !preferred
) {
696 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
700 return preferred
? preferred
: regular
;
703 unsigned long calc_csum(void *super
, int bytes
)
705 unsigned long long newcsum
= 0;
708 unsigned int *superc
= (unsigned int*) super
;
710 for(i
=0; i
<bytes
/4; i
++)
712 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
714 /* The in-kernel checksum calculation is always 16bit on
715 * the alpha, though it is 32 bit on i386...
716 * I wonder what it is elsewhere... (it uses and API in
717 * a way that it shouldn't).
719 csum
= (csum
& 0xffff) + (csum
>> 16);
720 csum
= (csum
& 0xffff) + (csum
>> 16);
726 char *human_size(long long bytes
)
730 /* We convert bytes to either centi-M{ega,ibi}bytes or
731 * centi-G{igi,ibi}bytes, with appropriate rounding,
732 * and then print 1/100th of those as a decimal.
733 * We allow upto 2048Megabytes before converting to
734 * gigabytes, as that shows more precision and isn't
735 * too large a number.
736 * Terrabytes are not yet handled.
739 if (bytes
< 5000*1024)
741 else if (bytes
< 2*1024LL*1024LL*1024LL) {
742 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
743 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
744 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
745 cMiB
/100 , cMiB
% 100,
748 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
749 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
750 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
751 cGiB
/100 , cGiB
% 100,
757 char *human_size_brief(long long bytes
)
761 if (bytes
< 5000*1024)
762 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
763 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
765 else if (bytes
< 2*1024LL*1024LL*1024LL)
766 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
768 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
771 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
773 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
778 void print_r10_layout(int layout
)
780 int near
= layout
& 255;
781 int far
= (layout
>> 8) & 255;
782 int offset
= (layout
&0x10000);
786 printf("%s near=%d", sep
, near
);
790 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
792 printf("NO REDUNDANCY");
796 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
797 int chunksize
, unsigned long long devsize
)
801 case 0: data_disks
= raid_disks
; break;
802 case 1: data_disks
= 1; break;
804 case 5: data_disks
= raid_disks
- 1; break;
805 case 6: data_disks
= raid_disks
- 2; break;
806 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
809 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
810 return data_disks
* devsize
;
813 int get_mdp_major(void)
815 static int mdp_major
= -1;
819 int have_devices
= 0;
824 fl
= fopen("/proc/devices", "r");
827 while ((w
= conf_word(fl
, 1))) {
828 if (have_block
&& strcmp(w
, "devices:")==0)
830 have_block
= (strcmp(w
, "Block")==0);
833 if (have_devices
&& strcmp(w
, "mdp")==0)
834 mdp_major
= last_num
;
841 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
842 char *get_md_name(int dev
)
844 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
845 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
846 static char devname
[50];
852 int mdp
= get_mdp_major();
853 if (mdp
< 0) return NULL
;
854 rdev
= makedev(mdp
, (-1-dev
)<<6);
855 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
856 if (stat(devname
, &stb
) == 0
857 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
858 && (stb
.st_rdev
== rdev
))
861 rdev
= makedev(MD_MAJOR
, dev
);
862 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
863 if (stat(devname
, &stb
) == 0
864 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
865 && (stb
.st_rdev
== rdev
))
868 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
869 if (stat(devname
, &stb
) == 0
870 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
871 && (stb
.st_rdev
== rdev
))
874 dn
= map_dev(major(rdev
), minor(rdev
), 0);
877 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
878 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
882 if (stat(devname
, &stb
) == 0
883 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
884 && (stb
.st_rdev
== rdev
))
890 void put_md_name(char *name
)
892 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
896 int find_free_devnum(int use_partitions
)
899 for (devnum
= 127; devnum
!= 128;
900 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
904 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
905 if (mddev_busy(_devnum
))
907 /* make sure it is new to /dev too, at least as a
909 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
910 if (dn
&& ! is_standard(dn
, NULL
))
916 return use_partitions
? (-1-devnum
) : devnum
;
918 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
920 int dev_open(char *dev
, int flags
)
922 /* like 'open', but if 'dev' matches %d:%d, create a temp
923 * block device and open that
933 major
= strtoul(dev
, &e
, 0);
934 if (e
> dev
&& *e
== ':' && e
[1] &&
935 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
937 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
938 (int)getpid(), major
, minor
);
939 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
940 fd
= open(devname
, flags
|O_DIRECT
);
944 fd
= open(dev
, flags
|O_DIRECT
);
948 int open_dev(int devnum
)
952 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
953 return dev_open(buf
, O_RDWR
);
956 int open_dev_excl(int devnum
)
961 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
962 for (i
=0 ; i
<25 ; i
++) {
963 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
973 int same_dev(char *one
, char *two
)
975 struct stat st1
, st2
;
976 if (stat(one
, &st1
) != 0)
978 if (stat(two
, &st2
) != 0)
980 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
982 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
984 return st1
.st_rdev
== st2
.st_rdev
;
987 void wait_for(char *dev
, int fd
)
990 struct stat stb_want
;
992 if (fstat(fd
, &stb_want
) != 0 ||
993 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
996 for (i
=0 ; i
<25 ; i
++) {
998 if (stat(dev
, &stb
) == 0 &&
999 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1000 (stb
.st_rdev
== stb_want
.st_rdev
))
1005 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
1008 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
1010 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1012 struct supertype
*super_by_fd(int fd
)
1014 mdu_array_info_t array
;
1017 struct supertype
*st
= NULL
;
1022 char *subarray
= NULL
;
1024 sra
= sysfs_read(fd
, 0, GET_VERSION
);
1027 vers
= sra
->array
.major_version
;
1028 minor
= sra
->array
.minor_version
;
1029 verstr
= sra
->text_version
;
1031 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1032 array
.major_version
= array
.minor_version
= 0;
1033 vers
= array
.major_version
;
1034 minor
= array
.minor_version
;
1039 sprintf(version
, "%d.%d", vers
, minor
);
1042 if (minor
== -2 && is_subarray(verstr
)) {
1043 char *dev
= verstr
+1;
1044 subarray
= strchr(dev
, '/');
1048 devnum
= devname2devnum(dev
);
1049 subarray
= strdup(subarray
);
1052 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
1053 if (sra
&& sra
->text_version
[0])
1054 verstr
= sra
->text_version
;
1056 verstr
= "-no-metadata-";
1059 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1060 st
= superlist
[i
]->match_metadata_desc(verstr
);
1067 strncpy(st
->subarray
, subarray
, 32);
1068 st
->subarray
[31] = 0;
1071 st
->subarray
[0] = 0;
1075 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1078 struct supertype
*dup_super(struct supertype
*orig
)
1080 struct supertype
*st
;
1084 st
= malloc(sizeof(*st
));
1087 memset(st
, 0, sizeof(*st
));
1089 st
->max_devs
= orig
->max_devs
;
1090 st
->minor_version
= orig
->minor_version
;
1091 strcpy(st
->subarray
, orig
->subarray
);
1097 struct supertype
*guess_super(int fd
)
1099 /* try each load_super to find the best match,
1100 * and return the best superswitch
1102 struct superswitch
*ss
;
1103 struct supertype
*st
;
1104 unsigned long besttime
= 0;
1108 st
= malloc(sizeof(*st
));
1109 for (i
=0 ; superlist
[i
]; i
++) {
1112 memset(st
, 0, sizeof(*st
));
1113 rv
= ss
->load_super(st
, fd
, NULL
);
1116 st
->ss
->getinfo_super(st
, &info
);
1117 if (bestsuper
== -1 ||
1118 besttime
< info
.array
.ctime
) {
1120 besttime
= info
.array
.ctime
;
1125 if (bestsuper
!= -1) {
1127 memset(st
, 0, sizeof(*st
));
1128 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1130 superlist
[bestsuper
]->free_super(st
);
1138 /* Return size of device in bytes */
1139 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1141 unsigned long long ldsize
;
1144 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1145 ldsize
= (unsigned long long)st
.st_size
;
1148 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1151 unsigned long dsize
;
1152 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1157 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1158 dname
, strerror(errno
));
1167 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1168 * Returns: 1 if successful
1169 * -1 for unknown partition type
1170 * 0 for other errors
1172 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1175 unsigned char buf
[512];
1176 unsigned char empty_gpt_entry
[16]= {0};
1177 struct GPT_part_entry
*part
;
1178 unsigned long long curr_part_end
;
1179 unsigned all_partitions
, entry_size
;
1184 BUILD_BUG_ON(sizeof(gpt
) != 512);
1185 /* read GPT header */
1186 lseek(fd
, 512, SEEK_SET
);
1187 if (read(fd
, &gpt
, 512) != 512)
1190 /* get the number of partition entries and the entry size */
1191 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1192 entry_size
= __le32_to_cpu(gpt
.part_size
);
1194 /* Check GPT signature*/
1195 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1199 if (all_partitions
> 1024 ||
1203 /* read first GPT partition entries */
1204 if (read(fd
, buf
, 512) != 512)
1207 part
= (struct GPT_part_entry
*)buf
;
1209 for (part_nr
=0; part_nr
< all_partitions
; part_nr
++) {
1210 /* is this valid partition? */
1211 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1212 /* check the last lba for the current partition */
1213 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1214 if (curr_part_end
> *endofpart
)
1215 *endofpart
= curr_part_end
;
1218 part
= (struct GPT_part_entry
*)((unsigned char*)part
+ entry_size
);
1220 if ((unsigned char *)part
>= buf
+ 512) {
1221 if (read(fd
, buf
, 512) != 512)
1223 part
= (struct GPT_part_entry
*)buf
;
1229 /* Sets endofpart parameter to the last block used by the last partition on the device.
1230 * Returns: 1 if successful
1231 * -1 for unknown partition type
1232 * 0 for other errors
1234 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1236 struct MBR boot_sect
;
1237 struct MBR_part_record
*part
;
1238 unsigned long long curr_part_end
;
1244 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1247 if (read(fd
, &boot_sect
, 512) != 512)
1250 /* check MBP signature */
1251 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1253 /* found the correct signature */
1254 part
= boot_sect
.parts
;
1256 for (part_nr
=0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1257 /* check for GPT type */
1258 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1259 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1262 /* check the last used lba for the current partition */
1263 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1264 __le32_to_cpu(part
->blocks_num
);
1265 if (curr_part_end
> *endofpart
)
1266 *endofpart
= curr_part_end
;
1271 /* Unknown partition table */
1278 int check_partitions(int fd
, char *dname
, unsigned long long freesize
)
1281 * Check where the last partition ends
1283 unsigned long long endofpart
;
1286 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1287 /* There appears to be a partition table here */
1288 if (freesize
== 0) {
1289 /* partitions will not be visible in new device */
1291 Name
": partition table exists on %s but will be lost or\n"
1292 " meaningless after creating array\n",
1295 } else if (endofpart
> freesize
) {
1296 /* last partition overlaps metadata */
1298 Name
": metadata will over-write last partition on %s.\n",
1306 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1309 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1310 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1311 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1315 int open_container(int fd
)
1317 /* 'fd' is a block device. Find out if it is in use
1318 * by a container, and return an open fd on that container.
1329 if (fstat(fd
, &st
) != 0)
1331 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1332 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1333 e
= path
+ strlen(path
);
1335 dir
= opendir(path
);
1338 while ((de
= readdir(dir
))) {
1341 if (de
->d_name
[0] == '.')
1343 sprintf(e
, "/%s/dev", de
->d_name
);
1344 dfd
= open(path
, O_RDONLY
);
1347 n
= read(dfd
, buf
, sizeof(buf
));
1349 if (n
<= 0 || n
>= sizeof(buf
))
1352 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1354 sprintf(buf
, "%d:%d", major
, minor
);
1355 dfd
= dev_open(buf
, O_RDONLY
);
1365 int add_disk(int mdfd
, struct supertype
*st
,
1366 struct mdinfo
*sra
, struct mdinfo
*info
)
1368 /* Add a device to an array, in one of 2 ways. */
1371 if (st
->ss
->external
) {
1372 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1373 info
->recovery_start
= MaxSector
;
1375 info
->recovery_start
= 0;
1376 rv
= sysfs_add_disk(sra
, info
, 0);
1379 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1383 sd2
= malloc(sizeof(*sd2
));
1385 sd2
->next
= sra
->devs
;
1391 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1395 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1397 /* Initialise kernel's knowledge of array.
1398 * This varies between externally managed arrays
1401 int vers
= md_get_version(mdfd
);
1405 if (st
->ss
->external
)
1406 rv
= sysfs_set_array(info
, vers
);
1409 if ((vers
% 100) >= 1) { /* can use different versions */
1410 mdu_array_info_t inf
;
1411 memset(&inf
, 0, sizeof(inf
));
1412 inf
.major_version
= info
->array
.major_version
;
1413 inf
.minor_version
= info
->array
.minor_version
;
1414 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1416 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1420 unsigned long long min_recovery_start(struct mdinfo
*array
)
1422 /* find the minimum recovery_start in an array for metadata
1423 * formats that only record per-array recovery progress instead
1426 unsigned long long recovery_start
= MaxSector
;
1429 for (d
= array
->devs
; d
; d
= d
->next
)
1430 recovery_start
= min(recovery_start
, d
->recovery_start
);
1432 return recovery_start
;
1435 char *devnum2devname(int num
)
1439 sprintf(name
, "md%d", num
);
1441 sprintf(name
, "md_d%d", -1-num
);
1442 return strdup(name
);
1445 int devname2devnum(char *name
)
1449 if (strncmp(name
, "md_d", 4)==0)
1450 num
= -1-strtoul(name
+4, &ep
, 10);
1452 num
= strtoul(name
+2, &ep
, 10);
1456 int stat2devnum(struct stat
*st
)
1463 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1464 if (major(st
->st_rdev
) == MD_MAJOR
)
1465 return minor(st
->st_rdev
);
1466 else if (major(st
->st_rdev
) == get_mdp_major())
1467 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1469 /* must be an extended-minor partition. Look at the
1470 * /sys/dev/block/%d:%d link which must look like
1471 * ../../block/mdXXX/mdXXXpYY
1473 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1474 minor(st
->st_rdev
));
1475 n
= readlink(path
, link
, sizeof(link
)-1);
1479 cp
= strrchr(link
, '/');
1481 cp
= strchr(link
, '/');
1482 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1483 return devname2devnum(cp
+1);
1489 int fd2devnum(int fd
)
1492 if (fstat(fd
, &stb
) == 0)
1493 return stat2devnum(&stb
);
1497 char *pid_dir
= VAR_RUN
;
1499 int mdmon_pid(int devnum
)
1505 sprintf(path
, "%s/%s.pid", pid_dir
, devnum2devname(devnum
));
1506 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1510 n
= read(fd
, pid
, 9);
1517 int mdmon_running(int devnum
)
1519 int pid
= mdmon_pid(devnum
);
1522 if (kill(pid
, 0) == 0)
1527 int start_mdmon(int devnum
)
1541 if (check_env("MDADM_NO_MDMON"))
1544 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1548 sl
= strrchr(pathbuf
, '/');
1553 strcpy(sl
, "mdmon");
1559 /* FIXME yuk. CLOSE_EXEC?? */
1560 for (i
=3; i
< 100; i
++)
1562 for (i
=0; paths
[i
]; i
++)
1564 execl(paths
[i
], "mdmon",
1565 devnum2devname(devnum
),
1568 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1569 "Array remains readonly\n");
1571 default: /* parent - good */
1572 pid
= wait(&status
);
1573 if (pid
< 0 || status
!= 0)
1579 int check_env(char *name
)
1581 char *val
= getenv(name
);
1583 if (val
&& atoi(val
) == 1)
1589 __u32
random32(void)
1592 int rfd
= open("/dev/urandom", O_RDONLY
);
1593 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1601 int flush_metadata_updates(struct supertype
*st
)
1605 st
->update_tail
= NULL
;
1609 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1613 while (st
->updates
) {
1614 struct metadata_update
*mu
= st
->updates
;
1615 st
->updates
= mu
->next
;
1617 send_message(sfd
, mu
, 0);
1625 st
->update_tail
= NULL
;
1629 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1632 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1638 *st
->update_tail
= mu
;
1639 st
->update_tail
= &mu
->next
;
1641 #endif /* MDASSEMBLE */
1644 /* tinyc doesn't optimize this check in ioctl.h out ... */
1645 unsigned int __invalid_size_argument_for_IOC
= 0;