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 test_partition(int fd
)
307 /* Check if fd is a whole-disk or a partition.
308 * BLKPG will return EINVAL on a partition, and BLKPG_DEL_PARTITION
309 * will return ENXIO on an invalid partition number.
311 struct blkpg_ioctl_arg a
;
312 struct blkpg_partition p
;
313 a
.op
= BLKPG_DEL_PARTITION
;
315 a
.datalen
= sizeof(p
);
317 memset(a
.data
, 0, a
.datalen
);
319 if (ioctl(fd
, BLKPG
, &a
) == 0)
320 /* Very unlikely, but not a partition */
323 /* not a partition */
330 int enough(int level
, int raid_disks
, int layout
, int clean
,
331 char *avail
, int avail_disks
)
336 /* This is the tricky one - we need to check
337 * which actual disks are present.
339 copies
= (layout
&255)* ((layout
>>8) & 255);
342 /* there must be one of the 'copies' form 'first' */
348 first
= (first
+1) % raid_disks
;
353 } while (first
!= 0);
356 case LEVEL_MULTIPATH
:
357 return avail_disks
>= 1;
360 return avail_disks
== raid_disks
;
362 return avail_disks
>= 1;
366 return avail_disks
>= raid_disks
-1;
368 return avail_disks
>= raid_disks
;
371 return avail_disks
>= raid_disks
-2;
373 return avail_disks
>= raid_disks
;
379 int enough_fd(int fd
)
381 struct mdu_array_info_s array
;
382 struct mdu_disk_info_s disk
;
387 if (ioctl(fd
, GET_ARRAY_INFO
, &array
) != 0 ||
388 array
.raid_disks
<= 0)
390 avail
= calloc(array
.raid_disks
, 1);
391 for (i
=0; i
<array
.raid_disks
+ array
.nr_disks
; i
++) {
393 if (ioctl(fd
, GET_DISK_INFO
, &disk
) != 0)
395 if (! (disk
.state
& (1<<MD_DISK_SYNC
)))
397 if (disk
.raid_disk
< 0 || disk
.raid_disk
>= array
.raid_disks
)
400 avail
[disk
.raid_disk
] = 1;
402 /* This is used on an active array, so assume it is clean */
403 return enough(array
.level
, array
.raid_disks
, array
.layout
,
409 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
410 int same_uuid(int a
[4], int b
[4], int swapuuid
)
412 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
413 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
417 /* parse uuids are hostendian.
418 * uuid's from some superblocks are big-ending
419 * if there is a difference, we need to swap..
421 unsigned char *ac
= (unsigned char *)a
;
422 unsigned char *bc
= (unsigned char *)b
;
424 for (i
=0; i
<16; i
+= 4) {
425 if (ac
[i
+0] != bc
[i
+3] ||
426 ac
[i
+1] != bc
[i
+2] ||
427 ac
[i
+2] != bc
[i
+1] ||
441 void copy_uuid(void *a
, int b
[4], int swapuuid
)
444 /* parse uuids are hostendian.
445 * uuid's from some superblocks are big-ending
446 * if there is a difference, we need to swap..
448 unsigned char *ac
= (unsigned char *)a
;
449 unsigned char *bc
= (unsigned char *)b
;
451 for (i
=0; i
<16; i
+= 4) {
461 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
468 copy_uuid(uuid
, id
, swap
);
469 for (i
= 0; i
< 4; i
++) {
472 for (j
= 3; j
>= 0; j
--) {
473 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
481 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
483 // dirty hack to work around an issue with super1 superblocks...
484 // super1 superblocks need swapuuid set in order for assembly to
485 // work, but can't have it set if we want this printout to match
486 // all the other uuid printouts in super1.c, so we force swapuuid
487 // to 1 to make our printout match the rest of super1
488 return __fname_from_uuid(info
->uuid
, (st
->ss
== &super1
) ? 1 : st
->ss
->swapuuid
, buf
, sep
);
492 int check_ext2(int fd
, char *name
)
495 * Check for an ext2fs file system.
496 * Superblock is always 1K at 1K offset
498 * s_magic is le16 at 56 == 0xEF53
499 * report mtime - le32 at 44
501 * logblksize - le32 at 24
503 unsigned char sb
[1024];
506 if (lseek(fd
, 1024,0)!= 1024)
508 if (read(fd
, sb
, 1024)!= 1024)
510 if (sb
[56] != 0x53 || sb
[57] != 0xef)
513 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
514 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
515 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
516 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
518 fprintf(stderr
," size=%dK mtime=%s",
519 size
*(1<<bsize
), ctime(&mtime
));
523 int check_reiser(int fd
, char *name
)
526 * superblock is at 64K
528 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
531 unsigned char sb
[1024];
533 if (lseek(fd
, 64*1024, 0) != 64*1024)
535 if (read(fd
, sb
, 1024) != 1024)
537 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
538 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
540 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
541 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
542 fprintf(stderr
, " size = %luK\n", size
*4);
547 int check_raid(int fd
, char *name
)
552 struct supertype
*st
= guess_super(fd
);
555 st
->ss
->load_super(st
, fd
, name
);
556 /* Looks like a raid array .. */
557 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
559 st
->ss
->getinfo_super(st
, &info
);
560 st
->ss
->free_super(st
);
561 crtime
= info
.array
.ctime
;
562 level
= map_num(pers
, info
.array
.level
);
563 if (!level
) level
= "-unknown-";
564 fprintf(stderr
, " level=%s devices=%d ctime=%s",
565 level
, info
.array
.raid_disks
, ctime(&crtime
));
573 for (i
=0; i
<5; i
++) {
575 fprintf(stderr
, "%s%s", mesg
, add
);
577 if (fgets(buf
, 100, stdin
)==NULL
)
579 if (buf
[0]=='y' || buf
[0]=='Y')
581 if (buf
[0]=='n' || buf
[0]=='N')
585 fprintf(stderr
, Name
": assuming 'no'\n");
588 #endif /* MDASSEMBLE */
590 char *map_num(mapping_t
*map
, int num
)
600 int map_name(mapping_t
*map
, char *name
)
603 if (strcmp(map
->name
, name
)==0)
611 int is_standard(char *dev
, int *nump
)
613 /* tests if dev is a "standard" md dev name.
614 * i.e if the last component is "/dNN" or "/mdNN",
615 * where NN is a string of digits
616 * Returns 1 if a partitionable standard,
617 * -1 if non-partitonable,
618 * 0 if not a standard name.
620 char *d
= strrchr(dev
, '/');
625 if (strncmp(d
, "/d",2)==0)
626 d
+= 2, type
=1; /* /dev/md/dN{pM} */
627 else if (strncmp(d
, "/md_d", 5)==0)
628 d
+= 5, type
=1; /* /dev/md_dN{pM} */
629 else if (strncmp(d
, "/md", 3)==0)
630 d
+= 3, type
=-1; /* /dev/mdN */
631 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
632 d
+= 1, type
=-1; /* /dev/md/N */
642 if (nump
) *nump
= num
;
649 * convert a major/minor pair for a block device into a name in /dev, if possible.
650 * On the first call, walk /dev collecting name.
651 * Put them in a simple linked listfor now.
658 int devlist_ready
= 0;
660 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
664 if (S_ISLNK(stb
->st_mode
)) {
665 if (stat(name
, &st
) != 0)
670 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
671 char *n
= strdup(name
);
672 struct devmap
*dm
= malloc(sizeof(*dm
));
673 if (strncmp(n
, "/dev/./", 7)==0)
676 dm
->major
= major(stb
->st_rdev
);
677 dm
->minor
= minor(stb
->st_rdev
);
688 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
690 return add_dev(name
, stb
, flag
, NULL
);
692 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
694 return ftw(path
, add_dev_1
, nopenfd
);
697 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
701 #endif /* HAVE_FTW */
702 #endif /* HAVE_NFTW */
705 * Find a block device with the right major/minor number.
706 * If we find multiple names, choose the shortest.
707 * If we find a name in /dev/md/, we prefer that.
708 * This applies only to names for MD devices.
710 char *map_dev(int major
, int minor
, int create
)
713 char *regular
= NULL
, *preferred
=NULL
;
716 if (major
== 0 && minor
== 0)
720 if (!devlist_ready
) {
724 struct devmap
*d
= devlist
;
729 if (lstat(dev
, &stb
)==0 &&
730 S_ISLNK(stb
.st_mode
))
732 nftw(dev
, add_dev
, 10, FTW_PHYS
);
737 for (p
=devlist
; p
; p
=p
->next
)
738 if (p
->major
== major
&&
740 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
741 if (preferred
== NULL
||
742 strlen(p
->name
) < strlen(preferred
))
745 if (regular
== NULL
||
746 strlen(p
->name
) < strlen(regular
))
750 if (!regular
&& !preferred
&& !did_check
) {
754 if (create
&& !regular
&& !preferred
) {
756 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
760 return preferred
? preferred
: regular
;
763 unsigned long calc_csum(void *super
, int bytes
)
765 unsigned long long newcsum
= 0;
768 unsigned int *superc
= (unsigned int*) super
;
770 for(i
=0; i
<bytes
/4; i
++)
772 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
774 /* The in-kernel checksum calculation is always 16bit on
775 * the alpha, though it is 32 bit on i386...
776 * I wonder what it is elsewhere... (it uses and API in
777 * a way that it shouldn't).
779 csum
= (csum
& 0xffff) + (csum
>> 16);
780 csum
= (csum
& 0xffff) + (csum
>> 16);
786 char *human_size(long long bytes
)
790 /* We convert bytes to either centi-M{ega,ibi}bytes or
791 * centi-G{igi,ibi}bytes, with appropriate rounding,
792 * and then print 1/100th of those as a decimal.
793 * We allow upto 2048Megabytes before converting to
794 * gigabytes, as that shows more precision and isn't
795 * too large a number.
796 * Terrabytes are not yet handled.
799 if (bytes
< 5000*1024)
801 else if (bytes
< 2*1024LL*1024LL*1024LL) {
802 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
803 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
804 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
805 cMiB
/100 , cMiB
% 100,
808 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
809 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
810 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
811 cGiB
/100 , cGiB
% 100,
817 char *human_size_brief(long long bytes
)
821 if (bytes
< 5000*1024)
822 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
823 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
825 else if (bytes
< 2*1024LL*1024LL*1024LL)
826 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
828 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
831 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
833 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
838 void print_r10_layout(int layout
)
840 int near
= layout
& 255;
841 int far
= (layout
>> 8) & 255;
842 int offset
= (layout
&0x10000);
846 printf("%s near=%d", sep
, near
);
850 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
852 printf("NO REDUNDANCY");
856 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
857 int chunksize
, unsigned long long devsize
)
861 case 0: data_disks
= raid_disks
; break;
862 case 1: data_disks
= 1; break;
864 case 5: data_disks
= raid_disks
- 1; break;
865 case 6: data_disks
= raid_disks
- 2; break;
866 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
869 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
870 return data_disks
* devsize
;
873 int get_mdp_major(void)
875 static int mdp_major
= -1;
879 int have_devices
= 0;
884 fl
= fopen("/proc/devices", "r");
887 while ((w
= conf_word(fl
, 1))) {
888 if (have_block
&& strcmp(w
, "devices:")==0)
890 have_block
= (strcmp(w
, "Block")==0);
893 if (have_devices
&& strcmp(w
, "mdp")==0)
894 mdp_major
= last_num
;
901 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
902 char *get_md_name(int dev
)
904 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
905 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
906 static char devname
[50];
912 int mdp
= get_mdp_major();
913 if (mdp
< 0) return NULL
;
914 rdev
= makedev(mdp
, (-1-dev
)<<6);
915 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
916 if (stat(devname
, &stb
) == 0
917 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
918 && (stb
.st_rdev
== rdev
))
921 rdev
= makedev(MD_MAJOR
, dev
);
922 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
923 if (stat(devname
, &stb
) == 0
924 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
925 && (stb
.st_rdev
== rdev
))
928 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
929 if (stat(devname
, &stb
) == 0
930 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
931 && (stb
.st_rdev
== rdev
))
934 dn
= map_dev(major(rdev
), minor(rdev
), 0);
937 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
938 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
942 if (stat(devname
, &stb
) == 0
943 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
944 && (stb
.st_rdev
== rdev
))
950 void put_md_name(char *name
)
952 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
956 int find_free_devnum(int use_partitions
)
959 for (devnum
= 127; devnum
!= 128;
960 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
964 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
965 if (mddev_busy(_devnum
))
967 /* make sure it is new to /dev too, at least as a
969 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
970 if (dn
&& ! is_standard(dn
, NULL
))
976 return use_partitions
? (-1-devnum
) : devnum
;
978 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
980 int dev_open(char *dev
, int flags
)
982 /* like 'open', but if 'dev' matches %d:%d, create a temp
983 * block device and open that
994 major
= strtoul(dev
, &e
, 0);
995 if (e
> dev
&& *e
== ':' && e
[1] &&
996 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
998 char *path
= map_dev(major
, minor
, 0);
1000 fd
= open(path
, flags
);
1002 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
1003 (int)getpid(), major
, minor
);
1004 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
1005 fd
= open(devname
, flags
);
1010 snprintf(devname
, sizeof(devname
), "/tmp/.tmp.md.%d:%d:%d",
1011 (int)getpid(), major
, minor
);
1012 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
1013 fd
= open(devname
, flags
);
1018 fd
= open(dev
, flags
);
1022 int open_dev(int devnum
)
1026 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
1027 return dev_open(buf
, O_RDONLY
);
1030 int open_dev_excl(int devnum
)
1035 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
1036 for (i
=0 ; i
<25 ; i
++) {
1037 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
1047 int same_dev(char *one
, char *two
)
1049 struct stat st1
, st2
;
1050 if (stat(one
, &st1
) != 0)
1052 if (stat(two
, &st2
) != 0)
1054 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
1056 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
1058 return st1
.st_rdev
== st2
.st_rdev
;
1061 void wait_for(char *dev
, int fd
)
1064 struct stat stb_want
;
1066 if (fstat(fd
, &stb_want
) != 0 ||
1067 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
1070 for (i
=0 ; i
<25 ; i
++) {
1072 if (stat(dev
, &stb
) == 0 &&
1073 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
1074 (stb
.st_rdev
== stb_want
.st_rdev
))
1079 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
1082 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
1084 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
1086 struct supertype
*super_by_fd(int fd
)
1088 mdu_array_info_t array
;
1091 struct supertype
*st
= NULL
;
1096 char *subarray
= NULL
;
1098 sra
= sysfs_read(fd
, 0, GET_VERSION
);
1101 vers
= sra
->array
.major_version
;
1102 minor
= sra
->array
.minor_version
;
1103 verstr
= sra
->text_version
;
1105 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1106 array
.major_version
= array
.minor_version
= 0;
1107 vers
= array
.major_version
;
1108 minor
= array
.minor_version
;
1113 sprintf(version
, "%d.%d", vers
, minor
);
1116 if (minor
== -2 && is_subarray(verstr
)) {
1117 char *dev
= verstr
+1;
1118 subarray
= strchr(dev
, '/');
1122 devnum
= devname2devnum(dev
);
1123 subarray
= strdup(subarray
);
1126 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
1127 if (sra
&& sra
->text_version
[0])
1128 verstr
= sra
->text_version
;
1130 verstr
= "-no-metadata-";
1133 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1134 st
= superlist
[i
]->match_metadata_desc(verstr
);
1141 strncpy(st
->subarray
, subarray
, 32);
1142 st
->subarray
[31] = 0;
1145 st
->subarray
[0] = 0;
1149 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1152 struct supertype
*dup_super(struct supertype
*orig
)
1154 struct supertype
*st
;
1158 st
= malloc(sizeof(*st
));
1161 memset(st
, 0, sizeof(*st
));
1163 st
->max_devs
= orig
->max_devs
;
1164 st
->minor_version
= orig
->minor_version
;
1165 strcpy(st
->subarray
, orig
->subarray
);
1171 struct supertype
*guess_super(int fd
)
1173 /* try each load_super to find the best match,
1174 * and return the best superswitch
1176 struct superswitch
*ss
;
1177 struct supertype
*st
;
1178 time_t besttime
= 0;
1182 st
= malloc(sizeof(*st
));
1183 for (i
=0 ; superlist
[i
]; i
++) {
1186 memset(st
, 0, sizeof(*st
));
1187 rv
= ss
->load_super(st
, fd
, NULL
);
1190 st
->ss
->getinfo_super(st
, &info
);
1191 if (bestsuper
== -1 ||
1192 besttime
< info
.array
.ctime
) {
1194 besttime
= info
.array
.ctime
;
1199 if (bestsuper
!= -1) {
1201 memset(st
, 0, sizeof(*st
));
1202 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1204 superlist
[bestsuper
]->free_super(st
);
1212 /* Return size of device in bytes */
1213 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1215 unsigned long long ldsize
;
1218 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1219 ldsize
= (unsigned long long)st
.st_size
;
1222 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1225 unsigned long dsize
;
1226 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1231 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1232 dname
, strerror(errno
));
1241 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1242 * Returns: 1 if successful
1243 * -1 for unknown partition type
1244 * 0 for other errors
1246 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1249 unsigned char buf
[512];
1250 unsigned char empty_gpt_entry
[16]= {0};
1251 struct GPT_part_entry
*part
;
1252 unsigned long long curr_part_end
;
1253 unsigned all_partitions
, entry_size
;
1258 BUILD_BUG_ON(sizeof(gpt
) != 512);
1259 /* read GPT header */
1260 lseek(fd
, 512, SEEK_SET
);
1261 if (read(fd
, &gpt
, 512) != 512)
1264 /* get the number of partition entries and the entry size */
1265 all_partitions
= __le32_to_cpu(gpt
.part_cnt
);
1266 entry_size
= __le32_to_cpu(gpt
.part_size
);
1268 /* Check GPT signature*/
1269 if (gpt
.magic
!= GPT_SIGNATURE_MAGIC
)
1273 if (all_partitions
> 1024 ||
1277 /* read first GPT partition entries */
1278 if (read(fd
, buf
, 512) != 512)
1281 part
= (struct GPT_part_entry
*)buf
;
1283 for (part_nr
=0; part_nr
< all_partitions
; part_nr
++) {
1284 /* is this valid partition? */
1285 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1286 /* check the last lba for the current partition */
1287 curr_part_end
= __le64_to_cpu(part
->ending_lba
);
1288 if (curr_part_end
> *endofpart
)
1289 *endofpart
= curr_part_end
;
1292 part
= (struct GPT_part_entry
*)((unsigned char*)part
+ entry_size
);
1294 if ((unsigned char *)part
>= buf
+ 512) {
1295 if (read(fd
, buf
, 512) != 512)
1297 part
= (struct GPT_part_entry
*)buf
;
1303 /* Sets endofpart parameter to the last block used by the last partition on the device.
1304 * Returns: 1 if successful
1305 * -1 for unknown partition type
1306 * 0 for other errors
1308 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1310 struct MBR boot_sect
;
1311 struct MBR_part_record
*part
;
1312 unsigned long long curr_part_end
;
1318 BUILD_BUG_ON(sizeof(boot_sect
) != 512);
1321 if (read(fd
, &boot_sect
, 512) != 512)
1324 /* check MBP signature */
1325 if (boot_sect
.magic
== MBR_SIGNATURE_MAGIC
) {
1327 /* found the correct signature */
1328 part
= boot_sect
.parts
;
1330 for (part_nr
=0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1331 /* check for GPT type */
1332 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1333 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1336 /* check the last used lba for the current partition */
1337 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1338 __le32_to_cpu(part
->blocks_num
);
1339 if (curr_part_end
> *endofpart
)
1340 *endofpart
= curr_part_end
;
1345 /* Unknown partition table */
1352 int check_partitions(int fd
, char *dname
, unsigned long long freesize
)
1355 * Check where the last partition ends
1357 unsigned long long endofpart
;
1360 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1361 /* There appears to be a partition table here */
1362 if (freesize
== 0) {
1363 /* partitions will not be visible in new device */
1365 Name
": partition table exists on %s but will be lost or\n"
1366 " meaningless after creating array\n",
1369 } else if (endofpart
> freesize
) {
1370 /* last partition overlaps metadata */
1372 Name
": metadata will over-write last partition on %s.\n",
1380 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1383 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1384 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1385 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1389 int open_container(int fd
)
1391 /* 'fd' is a block device. Find out if it is in use
1392 * by a container, and return an open fd on that container.
1403 if (fstat(fd
, &st
) != 0)
1405 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1406 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1407 e
= path
+ strlen(path
);
1409 dir
= opendir(path
);
1412 while ((de
= readdir(dir
))) {
1415 if (de
->d_name
[0] == '.')
1417 sprintf(e
, "/%s/dev", de
->d_name
);
1418 dfd
= open(path
, O_RDONLY
);
1421 n
= read(dfd
, buf
, sizeof(buf
));
1423 if (n
<= 0 || (unsigned)n
>= sizeof(buf
))
1426 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1428 sprintf(buf
, "%d:%d", major
, minor
);
1429 dfd
= dev_open(buf
, O_RDONLY
);
1439 struct superswitch
*version_to_superswitch(char *vers
)
1443 for (i
= 0; superlist
[i
]; i
++) {
1444 struct superswitch
*ss
= superlist
[i
];
1446 if (strcmp(vers
, ss
->name
) == 0)
1453 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
1455 if (mdstat
->metadata_version
== NULL
||
1456 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
1457 !is_subarray(mdstat
->metadata_version
+9) ||
1458 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
1459 mdstat
->metadata_version
[10+strlen(container
)] != '/')
1465 int is_subarray_active(char *subarray
, char *container
)
1467 struct mdstat_ent
*mdstat
= mdstat_read(0, 0);
1468 struct mdstat_ent
*ent
;
1470 for (ent
= mdstat
; ent
; ent
= ent
->next
) {
1471 if (is_container_member(ent
, container
)) {
1472 char *inst
= &ent
->metadata_version
[10+strlen(container
)+1];
1474 if (strcmp(inst
, subarray
) == 0)
1479 free_mdstat(mdstat
);
1484 /* open_subarray - opens a subarray in a container
1485 * @dev: container device name
1486 * @st: supertype with only ->subarray set
1487 * @quiet: block reporting errors flag
1489 * On success returns an fd to a container and fills in *st
1491 int open_subarray(char *dev
, struct supertype
*st
, int quiet
)
1496 fd
= open(dev
, O_RDWR
|O_EXCL
);
1499 fprintf(stderr
, Name
": Couldn't open %s, aborting\n",
1504 st
->devnum
= fd2devnum(fd
);
1505 if (st
->devnum
== NoMdDev
) {
1508 Name
": Failed to determine device number for %s\n",
1513 mdi
= sysfs_read(fd
, st
->devnum
, GET_VERSION
|GET_LEVEL
);
1516 fprintf(stderr
, Name
": Failed to read sysfs for %s\n",
1521 if (mdi
->array
.level
!= UnSet
) {
1523 fprintf(stderr
, Name
": %s is not a container\n", dev
);
1527 st
->ss
= version_to_superswitch(mdi
->text_version
);
1531 Name
": Operation not supported for %s metadata\n",
1536 st
->devname
= devnum2devname(st
->devnum
);
1539 fprintf(stderr
, Name
": Failed to allocate device name\n");
1543 if (st
->ss
->load_super(st
, fd
, NULL
)) {
1545 fprintf(stderr
, Name
": Failed to find subarray-%s in %s\n",
1550 if (!st
->loaded_container
) {
1552 fprintf(stderr
, Name
": %s is not a container\n", dev
);
1560 st
->ss
->free_super(st
);
1576 int add_disk(int mdfd
, struct supertype
*st
,
1577 struct mdinfo
*sra
, struct mdinfo
*info
)
1579 /* Add a device to an array, in one of 2 ways. */
1582 if (st
->ss
->external
) {
1583 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1584 info
->recovery_start
= MaxSector
;
1586 info
->recovery_start
= 0;
1587 rv
= sysfs_add_disk(sra
, info
, 0);
1590 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1594 sd2
= malloc(sizeof(*sd2
));
1596 sd2
->next
= sra
->devs
;
1602 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1606 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1608 /* Initialise kernel's knowledge of array.
1609 * This varies between externally managed arrays
1612 int vers
= md_get_version(mdfd
);
1616 if (st
->ss
->external
)
1617 rv
= sysfs_set_array(info
, vers
);
1620 if ((vers
% 100) >= 1) { /* can use different versions */
1621 mdu_array_info_t inf
;
1622 memset(&inf
, 0, sizeof(inf
));
1623 inf
.major_version
= info
->array
.major_version
;
1624 inf
.minor_version
= info
->array
.minor_version
;
1625 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1627 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1631 unsigned long long min_recovery_start(struct mdinfo
*array
)
1633 /* find the minimum recovery_start in an array for metadata
1634 * formats that only record per-array recovery progress instead
1637 unsigned long long recovery_start
= MaxSector
;
1640 for (d
= array
->devs
; d
; d
= d
->next
)
1641 recovery_start
= min(recovery_start
, d
->recovery_start
);
1643 return recovery_start
;
1646 char *devnum2devname(int num
)
1650 sprintf(name
, "md%d", num
);
1652 sprintf(name
, "md_d%d", -1-num
);
1653 return strdup(name
);
1656 int devname2devnum(char *name
)
1660 if (strncmp(name
, "md_d", 4)==0)
1661 num
= -1-strtoul(name
+4, &ep
, 10);
1663 num
= strtoul(name
+2, &ep
, 10);
1667 int stat2devnum(struct stat
*st
)
1674 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1675 if (major(st
->st_rdev
) == MD_MAJOR
)
1676 return minor(st
->st_rdev
);
1677 else if (major(st
->st_rdev
) == (unsigned)get_mdp_major())
1678 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1680 /* must be an extended-minor partition. Look at the
1681 * /sys/dev/block/%d:%d link which must look like
1682 * ../../block/mdXXX/mdXXXpYY
1684 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1685 minor(st
->st_rdev
));
1686 n
= readlink(path
, link
, sizeof(link
)-1);
1690 cp
= strrchr(link
, '/');
1692 cp
= strrchr(link
, '/');
1693 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1694 return devname2devnum(cp
+1);
1700 int fd2devnum(int fd
)
1703 if (fstat(fd
, &stb
) == 0)
1704 return stat2devnum(&stb
);
1708 int mdmon_pid(int devnum
)
1714 char *devname
= devnum2devname(devnum
);
1716 sprintf(path
, "%s/%s.pid", MDMON_DIR
, devname
);
1719 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1723 n
= read(fd
, pid
, 9);
1730 int mdmon_running(int devnum
)
1732 int pid
= mdmon_pid(devnum
);
1735 if (kill(pid
, 0) == 0)
1740 int start_mdmon(int devnum
)
1754 if (check_env("MDADM_NO_MDMON"))
1757 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1761 sl
= strrchr(pathbuf
, '/');
1766 strcpy(sl
, "mdmon");
1772 /* FIXME yuk. CLOSE_EXEC?? */
1773 for (i
=3; i
< 100; i
++)
1775 for (i
=0; paths
[i
]; i
++)
1777 execl(paths
[i
], "mdmon",
1778 devnum2devname(devnum
),
1781 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1782 "Array remains readonly\n");
1784 default: /* parent - good */
1785 pid
= wait(&status
);
1786 if (pid
< 0 || status
!= 0)
1792 int check_env(char *name
)
1794 char *val
= getenv(name
);
1796 if (val
&& atoi(val
) == 1)
1802 __u32
random32(void)
1805 int rfd
= open("/dev/urandom", O_RDONLY
);
1806 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1814 int flush_metadata_updates(struct supertype
*st
)
1818 st
->update_tail
= NULL
;
1822 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1826 while (st
->updates
) {
1827 struct metadata_update
*mu
= st
->updates
;
1828 st
->updates
= mu
->next
;
1830 send_message(sfd
, mu
, 0);
1838 st
->update_tail
= NULL
;
1842 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1845 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1851 *st
->update_tail
= mu
;
1852 st
->update_tail
= &mu
->next
;
1854 #endif /* MDASSEMBLE */
1857 /* tinyc doesn't optimize this check in ioctl.h out ... */
1858 unsigned int __invalid_size_argument_for_IOC
= 0;