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 */
70 /* Force a compilation error if condition is true */
71 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
73 /* Force a compilation error if condition is true, but also produce a
74 result (of value 0 and type size_t), so the expression can be used
75 e.g. in a structure initializer (or where-ever else comma expressions
77 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
80 * Parse a 128 bit uuid in 4 integers
81 * format is 32 hexx nibbles with options :.<space> separator
82 * If not exactly 32 hex digits are found, return 0
85 int parse_uuid(char *str
, int uuid
[4])
87 int hit
= 0; /* number of Hex digIT */
90 for (i
=0; i
<4; i
++) uuid
[i
]=0;
96 else if (c
>='a' && c
<= 'f')
98 else if (c
>='A' && c
<= 'F')
100 else if (strchr(":. -", c
))
117 * Get the md version number.
118 * We use the RAID_VERSION ioctl if it is supported
119 * If not, but we have a block device with major '9', we assume
122 * Return version number as 24 but number - assume version parts
126 int md_get_version(int fd
)
131 if (fstat(fd
, &stb
)<0)
133 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
136 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
137 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
140 if (major(stb
.st_rdev
) == MD_MAJOR
)
145 int get_linux_version()
154 a
= strtoul(cp
, &cp
, 10);
155 if (*cp
!= '.') return -1;
156 b
= strtoul(cp
+1, &cp
, 10);
157 if (*cp
!= '.') return -1;
158 c
= strtoul(cp
+1, NULL
, 10);
160 return (a
*1000000)+(b
*1000)+c
;
164 long long parse_size(char *size
)
166 /* parse 'size' which should be a number optionally
167 * followed by 'K', 'M', or 'G'.
168 * Without a suffix, K is assumed.
169 * Number returned is in sectors (half-K)
172 long long s
= strtoll(size
, &c
, 10);
186 s
*= 1024 * 1024 * 2;
195 int parse_layout_10(char *layout
)
199 /* Parse the layout string for raid10 */
200 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
201 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
202 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
206 if (layout
[0] == 'n')
208 else if (layout
[0] == 'o')
209 rv
= 0x10000 + (copies
<<8) + 1;
211 rv
= 1 + (copies
<<8);
215 int parse_layout_faulty(char *layout
)
217 /* Parse the layout string for 'faulty' */
218 int ln
= strcspn(layout
, "0123456789");
219 char *m
= strdup(layout
);
222 mode
= map_name(faultylayout
, m
);
226 return mode
| (atoi(layout
+ln
)<< ModeShift
);
230 void remove_partitions(int fd
)
232 /* remove partitions from this block devices.
233 * This is used for components added to an array
235 #ifdef BLKPG_DEL_PARTITION
236 struct blkpg_ioctl_arg a
;
237 struct blkpg_partition p
;
239 a
.op
= BLKPG_DEL_PARTITION
;
241 a
.datalen
= sizeof(p
);
243 memset(a
.data
, 0, a
.datalen
);
244 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
245 ioctl(fd
, BLKPG
, &a
);
249 int test_partition(int fd
)
251 /* Check if fd is a whole-disk or a partition.
252 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
253 * will return ENXIO on an invalid partition number.
255 struct blkpg_ioctl_arg a
;
256 struct blkpg_partition p
;
257 a
.op
= BLKPG_DEL_PARTITION
;
259 a
.datalen
= sizeof(p
);
261 memset(a
.data
, 0, a
.datalen
);
263 if (ioctl(fd
, BLKPG
, &a
) == 0)
264 /* Very unlikely, but not a partition */
267 /* not a partition */
274 int enough(int level
, int raid_disks
, int layout
, int clean
,
275 char *avail
, int avail_disks
)
280 /* This is the tricky one - we need to check
281 * which actual disks are present.
283 copies
= (layout
&255)* ((layout
>>8) & 255);
286 /* there must be one of the 'copies' form 'first' */
292 first
= (first
+1) % raid_disks
;
297 } while (first
!= 0);
300 case LEVEL_MULTIPATH
:
301 return avail_disks
>= 1;
304 return avail_disks
== raid_disks
;
306 return avail_disks
>= 1;
310 return avail_disks
>= raid_disks
-1;
312 return avail_disks
>= raid_disks
;
315 return avail_disks
>= raid_disks
-2;
317 return avail_disks
>= raid_disks
;
323 int enough_fd(int fd
)
325 struct mdu_array_info_s array
;
326 struct mdu_disk_info_s disk
;
331 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
332 array
.raid_disks
<= 0)
334 avail
= calloc(array
.raid_disks
, 1);
335 for (i
=0; i
<array
.raid_disks
+ array
.nr_disks
; i
++) {
337 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
339 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
341 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
344 avail
[disk
.raid_disk
] = 1;
346 /* This is used on an active array, so assume it is clean */
347 return enough(array
.level
, array
.raid_disks
, array
.layout
,
353 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
354 int same_uuid(int a
[4], int b
[4], int swapuuid
)
356 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
357 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
361 /* parse uuids are hostendian.
362 * uuid's from some superblocks are big-ending
363 * if there is a difference, we need to swap..
365 unsigned char *ac
= (unsigned char *)a
;
366 unsigned char *bc
= (unsigned char *)b
;
368 for (i
=0; i
<16; i
+= 4) {
369 if (ac
[i
+0] != bc
[i
+3] ||
370 ac
[i
+1] != bc
[i
+2] ||
371 ac
[i
+2] != bc
[i
+1] ||
385 void copy_uuid(void *a
, int b
[4], int swapuuid
)
388 /* parse uuids are hostendian.
389 * uuid's from some superblocks are big-ending
390 * if there is a difference, we need to swap..
392 unsigned char *ac
= (unsigned char *)a
;
393 unsigned char *bc
= (unsigned char *)b
;
395 for (i
=0; i
<16; i
+= 4) {
405 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
412 copy_uuid(uuid
, id
, swap
);
413 for (i
= 0; i
< 4; i
++) {
416 for (j
= 3; j
>= 0; j
--) {
417 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
425 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
427 // dirty hack to work around an issue with super1 superblocks...
428 // super1 superblocks need swapuuid set in order for assembly to
429 // work, but can't have it set if we want this printout to match
430 // all the other uuid printouts in super1.c, so we force swapuuid
431 // to 1 to make our printout match the rest of super1
432 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
436 int check_ext2(int fd
, char *name
)
439 * Check for an ext2fs file system.
440 * Superblock is always 1K at 1K offset
442 * s_magic is le16 at 56 == 0xEF53
443 * report mtime - le32 at 44
445 * logblksize - le32 at 24
447 unsigned char sb
[1024];
450 if (lseek(fd
, 1024,0)!= 1024)
452 if (read(fd
, sb
, 1024)!= 1024)
454 if (sb
[56] != 0x53 || sb
[57] != 0xef)
457 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
458 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
459 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
460 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
462 fprintf(stderr
," size=%dK mtime=%s",
463 size
*(1<<bsize
), ctime(&mtime
));
467 int check_reiser(int fd
, char *name
)
470 * superblock is at 64K
472 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
475 unsigned char sb
[1024];
477 if (lseek(fd
, 64*1024, 0) != 64*1024)
479 if (read(fd
, sb
, 1024) != 1024)
481 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
482 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
484 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
485 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
486 fprintf(stderr
, " size = %luK\n", size
*4);
491 int check_raid(int fd
, char *name
)
496 struct supertype
*st
= guess_super(fd
);
499 st
->ss
->load_super(st
, fd
, name
);
500 /* Looks like a raid array .. */
501 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
503 st
->ss
->getinfo_super(st
, &info
, NULL
);
504 st
->ss
->free_super(st
);
505 crtime
= info
.array
.ctime
;
506 level
= map_num(pers
, info
.array
.level
);
507 if (!level
) level
= "-unknown-";
508 fprintf(stderr
, " level=%s devices=%d ctime=%s",
509 level
, info
.array
.raid_disks
, ctime(&crtime
));
517 for (i
=0; i
<5; i
++) {
519 fprintf(stderr
, "%s%s", mesg
, add
);
521 if (fgets(buf
, 100, stdin
)==NULL
)
523 if (buf
[0]=='y' || buf
[0]=='Y')
525 if (buf
[0]=='n' || buf
[0]=='N')
529 fprintf(stderr
, Name
": assuming 'no'\n");
532 #endif /* MDASSEMBLE */
534 char *map_num(mapping_t
*map
, int num
)
544 int map_name(mapping_t
*map
, char *name
)
547 if (strcmp(map
->name
, name
)==0)
555 int is_standard(char *dev
, int *nump
)
557 /* tests if dev is a "standard" md dev name.
558 * i.e if the last component is "/dNN" or "/mdNN",
559 * where NN is a string of digits
560 * Returns 1 if a partitionable standard,
561 * -1 if non-partitonable,
562 * 0 if not a standard name.
564 char *d
= strrchr(dev
, '/');
569 if (strncmp(d
, "/d",2)==0)
570 d
+= 2, type
=1; /* /dev/md/dN{pM} */
571 else if (strncmp(d
, "/md_d", 5)==0)
572 d
+= 5, type
=1; /* /dev/md_dN{pM} */
573 else if (strncmp(d
, "/md", 3)==0)
574 d
+= 3, type
=-1; /* /dev/mdN */
575 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
576 d
+= 1, type
=-1; /* /dev/md/N */
586 if (nump
) *nump
= num
;
593 * convert a major/minor pair for a block device into a name in /dev, if possible.
594 * On the first call, walk /dev collecting name.
595 * Put them in a simple linked listfor now.
602 int devlist_ready
= 0;
604 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
608 if (S_ISLNK(stb
->st_mode
)) {
609 if (stat(name
, &st
) != 0)
614 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
615 char *n
= strdup(name
);
616 struct devmap
*dm
= malloc(sizeof(*dm
));
617 if (strncmp(n
, "/dev/./", 7)==0)
620 dm
->major
= major(stb
->st_rdev
);
621 dm
->minor
= minor(stb
->st_rdev
);
632 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
634 return add_dev(name
, stb
, flag
, NULL
);
636 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
638 return ftw(path
, add_dev_1
, nopenfd
);
641 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
645 #endif /* HAVE_FTW */
646 #endif /* HAVE_NFTW */
649 * Find a block device with the right major/minor number.
650 * If we find multiple names, choose the shortest.
651 * If we find a name in /dev/md/, we prefer that.
652 * This applies only to names for MD devices.
654 char *map_dev(int major
, int minor
, int create
)
657 char *regular
= NULL
, *preferred
=NULL
;
660 if (major
== 0 && minor
== 0)
664 if (!devlist_ready
) {
668 struct devmap
*d
= devlist
;
673 if (lstat(dev
, &stb
)==0 &&
674 S_ISLNK(stb
.st_mode
))
676 nftw(dev
, add_dev
, 10, FTW_PHYS
);
681 for (p
=devlist
; p
; p
=p
->next
)
682 if (p
->major
== major
&&
684 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
685 if (preferred
== NULL
||
686 strlen(p
->name
) < strlen(preferred
))
689 if (regular
== NULL
||
690 strlen(p
->name
) < strlen(regular
))
694 if (!regular
&& !preferred
&& !did_check
) {
698 if (create
&& !regular
&& !preferred
) {
700 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
704 return preferred
? preferred
: regular
;
707 unsigned long calc_csum(void *super
, int bytes
)
709 unsigned long long newcsum
= 0;
712 unsigned int *superc
= (unsigned int*) super
;
714 for(i
=0; i
<bytes
/4; i
++)
716 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
718 /* The in-kernel checksum calculation is always 16bit on
719 * the alpha, though it is 32 bit on i386...
720 * I wonder what it is elsewhere... (it uses and API in
721 * a way that it shouldn't).
723 csum
= (csum
& 0xffff) + (csum
>> 16);
724 csum
= (csum
& 0xffff) + (csum
>> 16);
730 char *human_size(long long bytes
)
734 /* We convert bytes to either centi-M{ega,ibi}bytes or
735 * centi-G{igi,ibi}bytes, with appropriate rounding,
736 * and then print 1/100th of those as a decimal.
737 * We allow upto 2048Megabytes before converting to
738 * gigabytes, as that shows more precision and isn't
739 * too large a number.
740 * Terrabytes are not yet handled.
743 if (bytes
< 5000*1024)
745 else if (bytes
< 2*1024LL*1024LL*1024LL) {
746 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
747 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
748 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
749 cMiB
/100 , cMiB
% 100,
752 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
753 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
754 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
755 cGiB
/100 , cGiB
% 100,
761 char *human_size_brief(long long bytes
)
765 if (bytes
< 5000*1024)
766 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
767 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
769 else if (bytes
< 2*1024LL*1024LL*1024LL)
770 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
772 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
775 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
777 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
782 void print_r10_layout(int layout
)
784 int near
= layout
& 255;
785 int far
= (layout
>> 8) & 255;
786 int offset
= (layout
&0x10000);
790 printf("%s near=%d", sep
, near
);
794 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
796 printf("NO REDUNDANCY");
800 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
801 int chunksize
, unsigned long long devsize
)
805 case 0: data_disks
= raid_disks
; break;
806 case 1: data_disks
= 1; break;
808 case 5: data_disks
= raid_disks
- 1; break;
809 case 6: data_disks
= raid_disks
- 2; break;
810 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
813 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
814 return data_disks
* devsize
;
817 int get_mdp_major(void)
819 static int mdp_major
= -1;
823 int have_devices
= 0;
828 fl
= fopen("/proc/devices", "r");
831 while ((w
= conf_word(fl
, 1))) {
832 if (have_block
&& strcmp(w
, "devices:")==0)
834 have_block
= (strcmp(w
, "Block")==0);
837 if (have_devices
&& strcmp(w
, "mdp")==0)
838 mdp_major
= last_num
;
845 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
846 char *get_md_name(int dev
)
848 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
849 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
850 static char devname
[50];
856 int mdp
= get_mdp_major();
857 if (mdp
< 0) return NULL
;
858 rdev
= makedev(mdp
, (-1-dev
)<<6);
859 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
860 if (stat(devname
, &stb
) == 0
861 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
862 && (stb
.st_rdev
== rdev
))
865 rdev
= makedev(MD_MAJOR
, dev
);
866 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
867 if (stat(devname
, &stb
) == 0
868 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
869 && (stb
.st_rdev
== rdev
))
872 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
873 if (stat(devname
, &stb
) == 0
874 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
875 && (stb
.st_rdev
== rdev
))
878 dn
= map_dev(major(rdev
), minor(rdev
), 0);
881 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
882 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
886 if (stat(devname
, &stb
) == 0
887 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
888 && (stb
.st_rdev
== rdev
))
894 void put_md_name(char *name
)
896 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
900 int find_free_devnum(int use_partitions
)
903 for (devnum
= 127; devnum
!= 128;
904 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
908 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
909 if (mddev_busy(_devnum
))
911 /* make sure it is new to /dev too, at least as a
913 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
914 if (dn
&& ! is_standard(dn
, NULL
))
920 return use_partitions
? (-1-devnum
) : devnum
;
922 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
924 int dev_open(char *dev
, int flags
)
926 /* like 'open', but if 'dev' matches %d:%d, create a temp
927 * block device and open that
938 major
= strtoul(dev
, &e
, 0);
939 if (e
> dev
&& *e
== ':' && e
[1] &&
940 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
942 char *path
= map_dev(major
, minor
, 0);
944 fd
= open(path
, flags
);
946 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
947 (int)getpid(), major
, minor
);
948 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
949 fd
= open(devname
, flags
);
954 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
955 (int)getpid(), major
, minor
);
956 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
957 fd
= open(devname
, flags
);
962 fd
= open(dev
, flags
);
966 int open_dev(int devnum
)
970 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
971 return dev_open(buf
, O_RDWR
);
974 int open_dev_excl(int devnum
)
979 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
980 for (i
=0 ; i
<25 ; i
++) {
981 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
991 int same_dev(char *one
, char *two
)
993 struct stat st1
, st2
;
994 if (stat(one
, &st1
) != 0)
996 if (stat(two
, &st2
) != 0)
998 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
1000 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
1002 return st1
.st_rdev
== st2
.st_rdev
;
1005 void wait_for(char *dev
, int fd
)
1008 struct stat stb_want
;
1010 if (fstat(fd
, &stb_want
) != 0 ||
1011 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
1014 for (i
=0 ; i
<25 ; i
++) {
1016 if (stat(dev
, &stb
) == 0 &&
1017 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1018 (stb
.st_rdev
== stb_want
.st_rdev
))
1023 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
1026 struct superswitch
*superlist
[] =
1029 &super_ddf
, &super_imsm
,
1033 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1035 struct supertype
*super_by_fd(int fd
, char **subarrayp
)
1037 mdu_array_info_t array
;
1040 struct supertype
*st
= NULL
;
1045 char *subarray
= NULL
;
1047 sra
= sysfs_read(fd
, 0, GET_VERSION
);
1050 vers
= sra
->array
.major_version
;
1051 minor
= sra
->array
.minor_version
;
1052 verstr
= sra
->text_version
;
1054 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1055 array
.major_version
= array
.minor_version
= 0;
1056 vers
= array
.major_version
;
1057 minor
= array
.minor_version
;
1062 sprintf(version
, "%d.%d", vers
, minor
);
1065 if (minor
== -2 && is_subarray(verstr
)) {
1066 char *dev
= verstr
+1;
1067 subarray
= strchr(dev
, '/');
1071 devnum
= devname2devnum(dev
);
1072 subarray
= strdup(subarray
);
1075 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
1076 if (sra
&& sra
->text_version
[0])
1077 verstr
= sra
->text_version
;
1079 verstr
= "-no-metadata-";
1082 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1083 st
= superlist
[i
]->match_metadata_desc(verstr
);
1090 *subarrayp
= subarray
;
1095 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1097 int dev_size_from_id(unsigned int id
, unsigned long long *size
)
1102 sprintf(buf
, "%d:%d", major(id
), minor(id
));
1103 fd
= dev_open(buf
, O_RDONLY
);
1106 if (get_dev_size(fd
, NULL
, size
)) {
1114 struct supertype
*dup_super(struct supertype
*orig
)
1116 struct supertype
*st
;
1120 st
= malloc(sizeof(*st
));
1123 memset(st
, 0, sizeof(*st
));
1125 st
->max_devs
= orig
->max_devs
;
1126 st
->minor_version
= orig
->minor_version
;
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
= malloc(sizeof(*st
));
1144 memset(st
, 0, sizeof(*st
));
1145 st
->container_dev
= NoMdDev
;
1147 for (i
=0 ; superlist
[i
]; i
++) {
1150 if (guess_type
== guess_array
&& ss
->add_to_super
== NULL
)
1152 if (guess_type
== guess_partitions
&& ss
->add_to_super
!= NULL
)
1154 memset(st
, 0, sizeof(*st
));
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 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1172 superlist
[bestsuper
]->free_super(st
);
1180 /* Return size of device in bytes */
1181 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1183 unsigned long long ldsize
;
1186 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1187 ldsize
= (unsigned long long)st
.st_size
;
1190 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1193 unsigned long dsize
;
1194 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1199 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1200 dname
, strerror(errno
));
1208 /* Return true if this can only be a container, not a member device.
1209 * i.e. is and md device and size is zero
1211 int must_be_container(int fd
)
1213 unsigned long long size
;
1214 if (md_get_version(fd
) < 0)
1216 if (get_dev_size(fd
, NULL
, &size
) == 0)
1223 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1224 * Returns: 1 if successful
1225 * -1 for unknown partition type
1226 * 0 for other errors
1228 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1231 unsigned char buf
[512];
1232 unsigned char empty_gpt_entry
[16]= {0};
1233 struct GPT_part_entry
*part
;
1234 unsigned long long curr_part_end
;
1235 unsigned all_partitions
, entry_size
;
1240 BUILD_BUG_ON(sizeof(gpt
) != 512);
1241 /* read GPT header */
1242 lseek(fd
, 512, SEEK_SET
);
1243 if (read(fd
, &gpt
, 512) != 512)
1246 /* get the number of partition entries and the entry size */
1247 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1248 entry_size
= __le32_to_cpu(gpt
.part_size
);
1250 /* Check GPT signature*/
1251 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1255 if (all_partitions
> 1024 ||
1259 /* read first GPT partition entries */
1260 if (read(fd
, buf
, 512) != 512)
1263 part
= (struct GPT_part_entry
*)buf
;
1265 for (part_nr
=0; part_nr
< all_partitions
; part_nr
++) {
1266 /* is this valid partition? */
1267 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1268 /* check the last lba for the current partition */
1269 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1270 if (curr_part_end
> *endofpart
)
1271 *endofpart
= curr_part_end
;
1274 part
= (struct GPT_part_entry
*)((unsigned char*)part
+ entry_size
);
1276 if ((unsigned char *)part
>= buf
+ 512) {
1277 if (read(fd
, buf
, 512) != 512)
1279 part
= (struct GPT_part_entry
*)buf
;
1285 /* Sets endofpart parameter to the last block used by the last partition on the device.
1286 * Returns: 1 if successful
1287 * -1 for unknown partition type
1288 * 0 for other errors
1290 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1292 struct MBR boot_sect
;
1293 struct MBR_part_record
*part
;
1294 unsigned long long curr_part_end
;
1300 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1303 if (read(fd
, &boot_sect
, 512) != 512)
1306 /* check MBP signature */
1307 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1309 /* found the correct signature */
1310 part
= boot_sect
.parts
;
1312 for (part_nr
=0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1313 /* check for GPT type */
1314 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1315 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1318 /* check the last used lba for the current partition */
1319 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1320 __le32_to_cpu(part
->blocks_num
);
1321 if (curr_part_end
> *endofpart
)
1322 *endofpart
= curr_part_end
;
1327 /* Unknown partition table */
1334 int check_partitions(int fd
, char *dname
, unsigned long long freesize
)
1337 * Check where the last partition ends
1339 unsigned long long endofpart
;
1342 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1343 /* There appears to be a partition table here */
1344 if (freesize
== 0) {
1345 /* partitions will not be visible in new device */
1347 Name
": partition table exists on %s but will be lost or\n"
1348 " meaningless after creating array\n",
1351 } else if (endofpart
> freesize
) {
1352 /* last partition overlaps metadata */
1354 Name
": metadata will over-write last partition on %s.\n",
1362 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1365 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1366 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1367 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1371 int open_container(int fd
)
1373 /* 'fd' is a block device. Find out if it is in use
1374 * by a container, and return an open fd on that container.
1385 if (fstat(fd
, &st
) != 0)
1387 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1388 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1389 e
= path
+ strlen(path
);
1391 dir
= opendir(path
);
1394 while ((de
= readdir(dir
))) {
1397 if (de
->d_name
[0] == '.')
1399 sprintf(e
, "/%s/dev", de
->d_name
);
1400 dfd
= open(path
, O_RDONLY
);
1403 n
= read(dfd
, buf
, sizeof(buf
));
1405 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1408 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1410 sprintf(buf
, "%d:%d", major
, minor
);
1411 dfd
= dev_open(buf
, O_RDONLY
);
1421 struct superswitch
*version_to_superswitch(char *vers
)
1425 for (i
= 0; superlist
[i
]; i
++) {
1426 struct superswitch
*ss
= superlist
[i
];
1428 if (strcmp(vers
, ss
->name
) == 0)
1435 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1437 if (mdstat
->metadata_version
== NULL
||
1438 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1439 !is_subarray(mdstat
->metadata_version
+9) ||
1440 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
1441 mdstat
->metadata_version
[10+strlen(container
)] != '/')
1447 int is_subarray_active(char *subarray
, char *container
)
1449 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1450 struct mdstat_ent
*ent
;
1452 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
1453 if (is_container_member(ent
, container
)) {
1454 char *inst
= &ent
->metadata_version
[10+strlen(container
)+1];
1456 if (!subarray
|| strcmp(inst
, subarray
) == 0)
1461 free_mdstat(mdstat
);
1466 int is_container_active(char *container
)
1468 return is_subarray_active(NULL
, container
);
1471 /* open_subarray - opens a subarray in a container
1472 * @dev: container device name
1473 * @st: empty supertype
1474 * @quiet: block reporting errors flag
1476 * On success returns an fd to a container and fills in *st
1478 int open_subarray(char *dev
, char *subarray
, struct supertype
*st
, int quiet
)
1481 struct mdinfo
*info
;
1484 fd
= open(dev
, O_RDWR
|O_EXCL
);
1487 fprintf(stderr
, Name
": Couldn't open %s, aborting\n",
1492 st
->devnum
= fd2devnum(fd
);
1493 if (st
->devnum
== NoMdDev
) {
1496 Name
": Failed to determine device number for %s\n",
1501 mdi
= sysfs_read(fd
, st
->devnum
, GET_VERSION
|GET_LEVEL
);
1504 fprintf(stderr
, Name
": Failed to read sysfs for %s\n",
1509 if (mdi
->array
.level
!= UnSet
) {
1511 fprintf(stderr
, Name
": %s is not a container\n", dev
);
1515 st
->ss
= version_to_superswitch(mdi
->text_version
);
1519 Name
": Operation not supported for %s metadata\n",
1524 st
->devname
= devnum2devname(st
->devnum
);
1527 fprintf(stderr
, Name
": Failed to allocate device name\n");
1531 if (!st
->ss
->load_container
) {
1533 fprintf(stderr
, Name
": %s is not a container\n", dev
);
1537 if (st
->ss
->load_container(st
, fd
, NULL
)) {
1539 fprintf(stderr
, Name
": Failed to load metadata for %s\n",
1544 info
= st
->ss
->container_content(st
, subarray
);
1547 fprintf(stderr
, Name
": Failed to find subarray-%s in %s\n",
1557 st
->ss
->free_super(st
);
1573 int add_disk(int mdfd
, struct supertype
*st
,
1574 struct mdinfo
*sra
, struct mdinfo
*info
)
1576 /* Add a device to an array, in one of 2 ways. */
1579 if (st
->ss
->external
) {
1580 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1581 info
->recovery_start
= MaxSector
;
1583 info
->recovery_start
= 0;
1584 rv
= sysfs_add_disk(sra
, info
, 0);
1587 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1591 sd2
= malloc(sizeof(*sd2
));
1593 sd2
->next
= sra
->devs
;
1599 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1603 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1605 /* Initialise kernel's knowledge of array.
1606 * This varies between externally managed arrays
1609 int vers
= md_get_version(mdfd
);
1613 if (st
->ss
->external
)
1614 rv
= sysfs_set_array(info
, vers
);
1617 if ((vers
% 100) >= 1) { /* can use different versions */
1618 mdu_array_info_t inf
;
1619 memset(&inf
, 0, sizeof(inf
));
1620 inf
.major_version
= info
->array
.major_version
;
1621 inf
.minor_version
= info
->array
.minor_version
;
1622 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1624 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1628 unsigned long long min_recovery_start(struct mdinfo
*array
)
1630 /* find the minimum recovery_start in an array for metadata
1631 * formats that only record per-array recovery progress instead
1634 unsigned long long recovery_start
= MaxSector
;
1637 for (d
= array
->devs
; d
; d
= d
->next
)
1638 recovery_start
= min(recovery_start
, d
->recovery_start
);
1640 return recovery_start
;
1643 char *devnum2devname(int num
)
1647 sprintf(name
, "md%d", num
);
1649 sprintf(name
, "md_d%d", -1-num
);
1650 return strdup(name
);
1653 int devname2devnum(char *name
)
1657 if (strncmp(name
, "md_d", 4)==0)
1658 num
= -1-strtoul(name
+4, &ep
, 10);
1660 num
= strtoul(name
+2, &ep
, 10);
1664 int stat2devnum(struct stat
*st
)
1671 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1672 if (major(st
->st_rdev
) == MD_MAJOR
)
1673 return minor(st
->st_rdev
);
1674 else if (major(st
->st_rdev
) == (unsigned)get_mdp_major())
1675 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1677 /* must be an extended-minor partition. Look at the
1678 * /sys/dev/block/%d:%d link which must look like
1679 * ../../block/mdXXX/mdXXXpYY
1681 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1682 minor(st
->st_rdev
));
1683 n
= readlink(path
, link
, sizeof(link
)-1);
1687 cp
= strrchr(link
, '/');
1689 cp
= strchr(link
, '/');
1690 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1691 return devname2devnum(cp
+1);
1697 int fd2devnum(int fd
)
1700 if (fstat(fd
, &stb
) == 0)
1701 return stat2devnum(&stb
);
1705 int mdmon_pid(int devnum
)
1711 char *devname
= devnum2devname(devnum
);
1713 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devname
);
1716 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1720 n
= read(fd
, pid
, 9);
1727 int mdmon_running(int devnum
)
1729 int pid
= mdmon_pid(devnum
);
1732 if (kill(pid
, 0) == 0)
1737 int start_mdmon(int devnum
)
1751 if (check_env("MDADM_NO_MDMON"))
1754 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1758 sl
= strrchr(pathbuf
, '/');
1763 strcpy(sl
, "mdmon");
1769 /* FIXME yuk. CLOSE_EXEC?? */
1770 for (i
=3; i
< 100; i
++)
1772 for (i
=0; paths
[i
]; i
++)
1774 execl(paths
[i
], "mdmon",
1775 devnum2devname(devnum
),
1778 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1779 "Array remains readonly\n");
1781 default: /* parent - good */
1782 pid
= wait(&status
);
1783 if (pid
< 0 || status
!= 0)
1789 int check_env(char *name
)
1791 char *val
= getenv(name
);
1793 if (val
&& atoi(val
) == 1)
1799 __u32
random32(void)
1802 int rfd
= open("/dev/urandom", O_RDONLY
);
1803 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1811 int flush_metadata_updates(struct supertype
*st
)
1815 st
->update_tail
= NULL
;
1819 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1823 while (st
->updates
) {
1824 struct metadata_update
*mu
= st
->updates
;
1825 st
->updates
= mu
->next
;
1827 send_message(sfd
, mu
, 0);
1835 st
->update_tail
= NULL
;
1839 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1842 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1848 *st
->update_tail
= mu
;
1849 st
->update_tail
= &mu
->next
;
1851 #endif /* MDASSEMBLE */
1854 /* tinyc doesn't optimize this check in ioctl.h out ... */
1855 unsigned int __invalid_size_argument_for_IOC
= 0;