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
{
85 struct GPT_part_entry
{
86 unsigned char type_guid
[16];
87 unsigned char partition_guid
[16];
88 unsigned char starting_lba
[8];
89 unsigned char ending_lba
[8];
90 unsigned char attr_bits
[8];
91 unsigned char name
[72];
94 /* MBR/GPT magic numbers */
95 #define MBR_SIGNATURE_MAGIC __cpu_to_le16(0xAA55)
96 #define GPT_SIGNATURE_MAGIC __cpu_to_le64(0x5452415020494645ULL)
98 #define MBR_SIGNATURE_OFFSET 510
99 #define MBR_PARTITION_TABLE_OFFSET 446
100 #define MBR_PARTITIONS 4
101 #define MBR_GPT_PARTITION_TYPE 0xEE
102 #define GPT_ALL_PARTITIONS_OFFSET 80
103 #define GPT_ENTRY_SIZE_OFFSET 84
106 * Parse a 128 bit uuid in 4 integers
107 * format is 32 hexx nibbles with options :.<space> separator
108 * If not exactly 32 hex digits are found, return 0
111 int parse_uuid(char *str
, int uuid
[4])
113 int hit
= 0; /* number of Hex digIT */
116 for (i
=0; i
<4; i
++) uuid
[i
]=0;
118 while ((c
= *str
++)) {
120 if (c
>='0' && c
<='9')
122 else if (c
>='a' && c
<= 'f')
124 else if (c
>='A' && c
<= 'F')
126 else if (strchr(":. -", c
))
143 * Get the md version number.
144 * We use the RAID_VERSION ioctl if it is supported
145 * If not, but we have a block device with major '9', we assume
148 * Return version number as 24 but number - assume version parts
152 int md_get_version(int fd
)
157 if (fstat(fd
, &stb
)<0)
159 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
162 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
163 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
166 if (major(stb
.st_rdev
) == MD_MAJOR
)
171 int get_linux_version()
180 a
= strtoul(cp
, &cp
, 10);
181 if (*cp
!= '.') return -1;
182 b
= strtoul(cp
+1, &cp
, 10);
183 if (*cp
!= '.') return -1;
184 c
= strtoul(cp
+1, NULL
, 10);
186 return (a
*1000000)+(b
*1000)+c
;
190 long long parse_size(char *size
)
192 /* parse 'size' which should be a number optionally
193 * followed by 'K', 'M', or 'G'.
194 * Without a suffix, K is assumed.
195 * Number returned is in sectors (half-K)
198 long long s
= strtoll(size
, &c
, 10);
212 s
*= 1024 * 1024 * 2;
221 int parse_layout_10(char *layout
)
225 /* Parse the layout string for raid10 */
226 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
227 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
228 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
232 if (layout
[0] == 'n')
234 else if (layout
[0] == 'o')
235 rv
= 0x10000 + (copies
<<8) + 1;
237 rv
= 1 + (copies
<<8);
241 int parse_layout_faulty(char *layout
)
243 /* Parse the layout string for 'faulty' */
244 int ln
= strcspn(layout
, "0123456789");
245 char *m
= strdup(layout
);
248 mode
= map_name(faultylayout
, m
);
252 return mode
| (atoi(layout
+ln
)<< ModeShift
);
256 void remove_partitions(int fd
)
258 /* remove partitions from this block devices.
259 * This is used for components added to an array
261 #ifdef BLKPG_DEL_PARTITION
262 struct blkpg_ioctl_arg a
;
263 struct blkpg_partition p
;
265 a
.op
= BLKPG_DEL_PARTITION
;
267 a
.datalen
= sizeof(p
);
269 memset(a
.data
, 0, a
.datalen
);
270 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
271 ioctl(fd
, BLKPG
, &a
);
275 int enough(int level
, int raid_disks
, int layout
, int clean
,
276 char *avail
, int avail_disks
)
281 /* This is the tricky one - we need to check
282 * which actual disks are present.
284 copies
= (layout
&255)* ((layout
>>8) & 255);
287 /* there must be one of the 'copies' form 'first' */
293 first
= (first
+1) % raid_disks
;
298 } while (first
!= 0);
301 case LEVEL_MULTIPATH
:
302 return avail_disks
>= 1;
305 return avail_disks
== raid_disks
;
307 return avail_disks
>= 1;
311 return avail_disks
>= raid_disks
-1;
313 return avail_disks
>= raid_disks
;
316 return avail_disks
>= raid_disks
-2;
318 return avail_disks
>= raid_disks
;
324 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
325 int same_uuid(int a
[4], int b
[4], int swapuuid
)
327 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
328 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
332 /* parse uuids are hostendian.
333 * uuid's from some superblocks are big-ending
334 * if there is a difference, we need to swap..
336 unsigned char *ac
= (unsigned char *)a
;
337 unsigned char *bc
= (unsigned char *)b
;
339 for (i
=0; i
<16; i
+= 4) {
340 if (ac
[i
+0] != bc
[i
+3] ||
341 ac
[i
+1] != bc
[i
+2] ||
342 ac
[i
+2] != bc
[i
+1] ||
356 void copy_uuid(void *a
, int b
[4], int swapuuid
)
359 /* parse uuids are hostendian.
360 * uuid's from some superblocks are big-ending
361 * if there is a difference, we need to swap..
363 unsigned char *ac
= (unsigned char *)a
;
364 unsigned char *bc
= (unsigned char *)b
;
366 for (i
=0; i
<16; i
+= 4) {
376 char *__fname_from_uuid(int id
[4], int swap
, char *buf
, char sep
)
383 copy_uuid(uuid
, id
, swap
);
384 for (i
= 0; i
< 4; i
++) {
387 for (j
= 3; j
>= 0; j
--) {
388 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
396 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
398 return __fname_from_uuid(info
->uuid
, st
->ss
->swapuuid
, buf
, sep
);
402 int check_ext2(int fd
, char *name
)
405 * Check for an ext2fs file system.
406 * Superblock is always 1K at 1K offset
408 * s_magic is le16 at 56 == 0xEF53
409 * report mtime - le32 at 44
411 * logblksize - le32 at 24
413 unsigned char sb
[1024];
416 if (lseek(fd
, 1024,0)!= 1024)
418 if (read(fd
, sb
, 1024)!= 1024)
420 if (sb
[56] != 0x53 || sb
[57] != 0xef)
423 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
424 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
425 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
426 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
428 fprintf(stderr
," size=%dK mtime=%s",
429 size
*(1<<bsize
), ctime(&mtime
));
433 int check_reiser(int fd
, char *name
)
436 * superblock is at 64K
438 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
441 unsigned char sb
[1024];
443 if (lseek(fd
, 64*1024, 0) != 64*1024)
445 if (read(fd
, sb
, 1024) != 1024)
447 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
448 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
450 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
451 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
452 fprintf(stderr
, " size = %luK\n", size
*4);
457 int check_raid(int fd
, char *name
)
462 struct supertype
*st
= guess_super(fd
);
465 st
->ss
->load_super(st
, fd
, name
);
466 /* Looks like a raid array .. */
467 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
469 st
->ss
->getinfo_super(st
, &info
);
470 st
->ss
->free_super(st
);
471 crtime
= info
.array
.ctime
;
472 level
= map_num(pers
, info
.array
.level
);
473 if (!level
) level
= "-unknown-";
474 fprintf(stderr
, " level=%s devices=%d ctime=%s",
475 level
, info
.array
.raid_disks
, ctime(&crtime
));
483 for (i
=0; i
<5; i
++) {
485 fprintf(stderr
, "%s%s", mesg
, add
);
487 if (fgets(buf
, 100, stdin
)==NULL
)
489 if (buf
[0]=='y' || buf
[0]=='Y')
491 if (buf
[0]=='n' || buf
[0]=='N')
495 fprintf(stderr
, Name
": assuming 'no'\n");
498 #endif /* MDASSEMBLE */
500 char *map_num(mapping_t
*map
, int num
)
510 int map_name(mapping_t
*map
, char *name
)
513 if (strcmp(map
->name
, name
)==0)
521 int is_standard(char *dev
, int *nump
)
523 /* tests if dev is a "standard" md dev name.
524 * i.e if the last component is "/dNN" or "/mdNN",
525 * where NN is a string of digits
526 * Returns 1 if a partitionable standard,
527 * -1 if non-partitonable,
528 * 0 if not a standard name.
530 char *d
= strrchr(dev
, '/');
535 if (strncmp(d
, "/d",2)==0)
536 d
+= 2, type
=1; /* /dev/md/dN{pM} */
537 else if (strncmp(d
, "/md_d", 5)==0)
538 d
+= 5, type
=1; /* /dev/md_dN{pM} */
539 else if (strncmp(d
, "/md", 3)==0)
540 d
+= 3, type
=-1; /* /dev/mdN */
541 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
542 d
+= 1, type
=-1; /* /dev/md/N */
552 if (nump
) *nump
= num
;
559 * convert a major/minor pair for a block device into a name in /dev, if possible.
560 * On the first call, walk /dev collecting name.
561 * Put them in a simple linked listfor now.
568 int devlist_ready
= 0;
570 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
574 if (S_ISLNK(stb
->st_mode
)) {
575 if (stat(name
, &st
) != 0)
580 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
581 char *n
= strdup(name
);
582 struct devmap
*dm
= malloc(sizeof(*dm
));
583 if (strncmp(n
, "/dev/./", 7)==0)
586 dm
->major
= major(stb
->st_rdev
);
587 dm
->minor
= minor(stb
->st_rdev
);
598 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
600 return add_dev(name
, stb
, flag
, NULL
);
602 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
604 return ftw(path
, add_dev_1
, nopenfd
);
607 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
611 #endif /* HAVE_FTW */
612 #endif /* HAVE_NFTW */
615 * Find a block device with the right major/minor number.
616 * If we find multiple names, choose the shortest.
617 * If we find a name in /dev/md/, we prefer that.
618 * This applies only to names for MD devices.
620 char *map_dev(int major
, int minor
, int create
)
623 char *regular
= NULL
, *preferred
=NULL
;
626 if (major
== 0 && minor
== 0)
630 if (!devlist_ready
) {
634 struct devmap
*d
= devlist
;
639 if (lstat(dev
, &stb
)==0 &&
640 S_ISLNK(stb
.st_mode
))
642 nftw(dev
, add_dev
, 10, FTW_PHYS
);
647 for (p
=devlist
; p
; p
=p
->next
)
648 if (p
->major
== major
&&
650 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
651 if (preferred
== NULL
||
652 strlen(p
->name
) < strlen(preferred
))
655 if (regular
== NULL
||
656 strlen(p
->name
) < strlen(regular
))
660 if (!regular
&& !preferred
&& !did_check
) {
664 if (create
&& !regular
&& !preferred
) {
666 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
670 return preferred
? preferred
: regular
;
673 unsigned long calc_csum(void *super
, int bytes
)
675 unsigned long long newcsum
= 0;
678 unsigned int *superc
= (unsigned int*) super
;
680 for(i
=0; i
<bytes
/4; i
++)
682 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
684 /* The in-kernel checksum calculation is always 16bit on
685 * the alpha, though it is 32 bit on i386...
686 * I wonder what it is elsewhere... (it uses and API in
687 * a way that it shouldn't).
689 csum
= (csum
& 0xffff) + (csum
>> 16);
690 csum
= (csum
& 0xffff) + (csum
>> 16);
696 char *human_size(long long bytes
)
700 /* We convert bytes to either centi-M{ega,ibi}bytes or
701 * centi-G{igi,ibi}bytes, with appropriate rounding,
702 * and then print 1/100th of those as a decimal.
703 * We allow upto 2048Megabytes before converting to
704 * gigabytes, as that shows more precision and isn't
705 * too large a number.
706 * Terrabytes are not yet handled.
709 if (bytes
< 5000*1024)
711 else if (bytes
< 2*1024LL*1024LL*1024LL) {
712 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
713 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
714 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
715 cMiB
/100 , cMiB
% 100,
718 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
719 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
720 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
721 cGiB
/100 , cGiB
% 100,
727 char *human_size_brief(long long bytes
)
731 if (bytes
< 5000*1024)
732 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
733 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
735 else if (bytes
< 2*1024LL*1024LL*1024LL)
736 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
738 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
741 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
743 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
748 void print_r10_layout(int layout
)
750 int near
= layout
& 255;
751 int far
= (layout
>> 8) & 255;
752 int offset
= (layout
&0x10000);
756 printf("%s near=%d", sep
, near
);
760 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
762 printf("NO REDUNDANCY");
766 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
767 int chunksize
, unsigned long long devsize
)
771 case 0: data_disks
= raid_disks
; break;
772 case 1: data_disks
= 1; break;
774 case 5: data_disks
= raid_disks
- 1; break;
775 case 6: data_disks
= raid_disks
- 2; break;
776 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
779 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
780 return data_disks
* devsize
;
783 int get_mdp_major(void)
785 static int mdp_major
= -1;
789 int have_devices
= 0;
794 fl
= fopen("/proc/devices", "r");
797 while ((w
= conf_word(fl
, 1))) {
798 if (have_block
&& strcmp(w
, "devices:")==0)
800 have_block
= (strcmp(w
, "Block")==0);
803 if (have_devices
&& strcmp(w
, "mdp")==0)
804 mdp_major
= last_num
;
811 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
812 char *get_md_name(int dev
)
814 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
815 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
816 static char devname
[50];
822 int mdp
= get_mdp_major();
823 if (mdp
< 0) return NULL
;
824 rdev
= makedev(mdp
, (-1-dev
)<<6);
825 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
826 if (stat(devname
, &stb
) == 0
827 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
828 && (stb
.st_rdev
== rdev
))
831 rdev
= makedev(MD_MAJOR
, dev
);
832 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
833 if (stat(devname
, &stb
) == 0
834 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
835 && (stb
.st_rdev
== rdev
))
838 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
839 if (stat(devname
, &stb
) == 0
840 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
841 && (stb
.st_rdev
== rdev
))
844 dn
= map_dev(major(rdev
), minor(rdev
), 0);
847 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
848 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
852 if (stat(devname
, &stb
) == 0
853 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
854 && (stb
.st_rdev
== rdev
))
860 void put_md_name(char *name
)
862 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
866 int find_free_devnum(int use_partitions
)
869 for (devnum
= 127; devnum
!= 128;
870 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
874 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
875 if (mddev_busy(_devnum
))
877 /* make sure it is new to /dev too, at least as a
879 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
880 if (dn
&& ! is_standard(dn
, NULL
))
886 return use_partitions
? (-1-devnum
) : devnum
;
888 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
890 int dev_open(char *dev
, int flags
)
892 /* like 'open', but if 'dev' matches %d:%d, create a temp
893 * block device and open that
903 major
= strtoul(dev
, &e
, 0);
904 if (e
> dev
&& *e
== ':' && e
[1] &&
905 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
907 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
908 (int)getpid(), major
, minor
);
909 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
910 fd
= open(devname
, flags
|O_DIRECT
);
914 fd
= open(dev
, flags
|O_DIRECT
);
918 int open_dev(int devnum
)
922 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
923 return dev_open(buf
, O_RDWR
);
926 int open_dev_excl(int devnum
)
931 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
932 for (i
=0 ; i
<25 ; i
++) {
933 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
943 int same_dev(char *one
, char *two
)
945 struct stat st1
, st2
;
946 if (stat(one
, &st1
) != 0)
948 if (stat(two
, &st2
) != 0)
950 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
952 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
954 return st1
.st_rdev
== st2
.st_rdev
;
957 void wait_for(char *dev
, int fd
)
960 struct stat stb_want
;
962 if (fstat(fd
, &stb_want
) != 0 ||
963 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
966 for (i
=0 ; i
<25 ; i
++) {
968 if (stat(dev
, &stb
) == 0 &&
969 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
970 (stb
.st_rdev
== stb_want
.st_rdev
))
975 dprintf("%s: timeout waiting for %s\n", __func__
, dev
);
978 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
980 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
982 struct supertype
*super_by_fd(int fd
)
984 mdu_array_info_t array
;
987 struct supertype
*st
= NULL
;
992 char *subarray
= NULL
;
994 sra
= sysfs_read(fd
, 0, GET_VERSION
);
997 vers
= sra
->array
.major_version
;
998 minor
= sra
->array
.minor_version
;
999 verstr
= sra
->text_version
;
1001 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
1002 array
.major_version
= array
.minor_version
= 0;
1003 vers
= array
.major_version
;
1004 minor
= array
.minor_version
;
1009 sprintf(version
, "%d.%d", vers
, minor
);
1012 if (minor
== -2 && is_subarray(verstr
)) {
1013 char *dev
= verstr
+1;
1014 subarray
= strchr(dev
, '/');
1018 devnum
= devname2devnum(dev
);
1019 subarray
= strdup(subarray
);
1022 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
1023 if (sra
&& sra
->text_version
[0])
1024 verstr
= sra
->text_version
;
1026 verstr
= "-no-metadata-";
1029 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
1030 st
= superlist
[i
]->match_metadata_desc(verstr
);
1037 strncpy(st
->subarray
, subarray
, 32);
1038 st
->subarray
[31] = 0;
1041 st
->subarray
[0] = 0;
1045 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1048 struct supertype
*dup_super(struct supertype
*orig
)
1050 struct supertype
*st
;
1054 st
= malloc(sizeof(*st
));
1057 memset(st
, 0, sizeof(*st
));
1059 st
->max_devs
= orig
->max_devs
;
1060 st
->minor_version
= orig
->minor_version
;
1061 strcpy(st
->subarray
, orig
->subarray
);
1067 struct supertype
*guess_super(int fd
)
1069 /* try each load_super to find the best match,
1070 * and return the best superswitch
1072 struct superswitch
*ss
;
1073 struct supertype
*st
;
1074 unsigned long besttime
= 0;
1078 st
= malloc(sizeof(*st
));
1079 for (i
=0 ; superlist
[i
]; i
++) {
1082 memset(st
, 0, sizeof(*st
));
1083 rv
= ss
->load_super(st
, fd
, NULL
);
1086 st
->ss
->getinfo_super(st
, &info
);
1087 if (bestsuper
== -1 ||
1088 besttime
< info
.array
.ctime
) {
1090 besttime
= info
.array
.ctime
;
1095 if (bestsuper
!= -1) {
1097 memset(st
, 0, sizeof(*st
));
1098 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1100 superlist
[bestsuper
]->free_super(st
);
1108 /* Return size of device in bytes */
1109 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1111 unsigned long long ldsize
;
1114 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1115 ldsize
= (unsigned long long)st
.st_size
;
1118 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1121 unsigned long dsize
;
1122 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1127 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1128 dname
, strerror(errno
));
1137 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
1138 * Returns: 1 if successful
1139 * -1 for unknown partition type
1140 * 0 for other errors
1142 static int get_gpt_last_partition_end(int fd
, unsigned long long *endofpart
)
1144 unsigned char buf
[512];
1145 unsigned char empty_gpt_entry
[16]= {0};
1146 struct GPT_part_entry
*part
;
1147 unsigned long long curr_part_end
;
1148 unsigned all_partitions
, entry_size
;
1153 /* read GPT header */
1154 lseek(fd
, 512, SEEK_SET
);
1155 if (read(fd
, buf
, 512) != 512)
1158 /* get the number of partition entries and the entry size */
1159 all_partitions
= __le32_to_cpu(buf
[GPT_ALL_PARTITIONS_OFFSET
]);
1160 entry_size
= __le32_to_cpu(buf
[GPT_ENTRY_SIZE_OFFSET
]);
1162 /* Check GPT signature*/
1163 if (*((__u64
*)buf
) != GPT_SIGNATURE_MAGIC
)
1167 if (all_partitions
> 1024 ||
1171 /* read first GPT partition entries */
1172 if (read(fd
, buf
, 512) != 512)
1175 part
= (struct GPT_part_entry
*)buf
;
1177 for (part_nr
=0; part_nr
< all_partitions
; part_nr
++) {
1178 /* is this valid partition? */
1179 if (memcmp(part
->type_guid
, empty_gpt_entry
, 16) != 0) {
1180 /* check the last lba for the current partition */
1181 curr_part_end
= __le64_to_cpu(*(__u64
*)part
->ending_lba
);
1182 if (curr_part_end
> *endofpart
)
1183 *endofpart
= curr_part_end
;
1186 part
= (struct GPT_part_entry
*)((unsigned char*)part
+ entry_size
);
1188 if ((unsigned char *)part
>= buf
+ 512) {
1189 if (read(fd
, buf
, 512) != 512)
1191 part
= (struct GPT_part_entry
*)buf
;
1197 /* Sets endofpart parameter to the last block used by the last partition on the device.
1198 * Returns: 1 if successful
1199 * -1 for unknown partition type
1200 * 0 for other errors
1202 static int get_last_partition_end(int fd
, unsigned long long *endofpart
)
1204 unsigned char boot_sect
[512];
1205 struct MBR_part_record
*part
;
1206 unsigned long long curr_part_end
;
1214 if (read(fd
, boot_sect
, 512) != 512)
1217 /* check MBP signature */
1218 if (*((__u16
*)(boot_sect
+ MBR_SIGNATURE_OFFSET
))
1219 == MBR_SIGNATURE_MAGIC
) {
1221 /* found the correct signature */
1222 part
= (struct MBR_part_record
*)
1223 (boot_sect
+ MBR_PARTITION_TABLE_OFFSET
);
1225 for (part_nr
=0; part_nr
< MBR_PARTITIONS
; part_nr
++) {
1226 /* check for GPT type */
1227 if (part
->part_type
== MBR_GPT_PARTITION_TYPE
) {
1228 retval
= get_gpt_last_partition_end(fd
, endofpart
);
1231 /* check the last used lba for the current partition */
1232 curr_part_end
= __le32_to_cpu(part
->first_sect_lba
) +
1233 __le32_to_cpu(part
->blocks_num
);
1234 if (curr_part_end
> *endofpart
)
1235 *endofpart
= curr_part_end
;
1240 /* Unknown partition table */
1247 int check_partitions(int fd
, char *dname
, unsigned long long freesize
)
1250 * Check where the last partition ends
1252 unsigned long long endofpart
;
1255 if ((ret
= get_last_partition_end(fd
, &endofpart
)) > 0) {
1256 /* There appears to be a partition table here */
1257 if (freesize
== 0) {
1258 /* partitions will not be visible in new device */
1260 Name
": partition table exists on %s but will be lost or\n"
1261 " meaningless after creating array\n",
1264 } else if (endofpart
> freesize
) {
1265 /* last partition overlaps metadata */
1267 Name
": metadata will over-write last partition on %s.\n",
1275 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1278 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1279 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1280 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1284 int open_container(int fd
)
1286 /* 'fd' is a block device. Find out if it is in use
1287 * by a container, and return an open fd on that container.
1298 if (fstat(fd
, &st
) != 0)
1300 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1301 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1302 e
= path
+ strlen(path
);
1304 dir
= opendir(path
);
1307 while ((de
= readdir(dir
))) {
1310 if (de
->d_name
[0] == '.')
1312 sprintf(e
, "/%s/dev", de
->d_name
);
1313 dfd
= open(path
, O_RDONLY
);
1316 n
= read(dfd
, buf
, sizeof(buf
));
1318 if (n
<= 0 || n
>= sizeof(buf
))
1321 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1323 sprintf(buf
, "%d:%d", major
, minor
);
1324 dfd
= dev_open(buf
, O_RDONLY
);
1334 int add_disk(int mdfd
, struct supertype
*st
,
1335 struct mdinfo
*sra
, struct mdinfo
*info
)
1337 /* Add a device to an array, in one of 2 ways. */
1340 if (st
->ss
->external
) {
1341 if (info
->disk
.state
& (1<<MD_DISK_SYNC
))
1342 info
->recovery_start
= MaxSector
;
1344 info
->recovery_start
= 0;
1345 rv
= sysfs_add_disk(sra
, info
, 0);
1348 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1352 sd2
= malloc(sizeof(*sd2
));
1354 sd2
->next
= sra
->devs
;
1360 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1364 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1366 /* Initialise kernel's knowledge of array.
1367 * This varies between externally managed arrays
1370 int vers
= md_get_version(mdfd
);
1374 if (st
->ss
->external
)
1375 rv
= sysfs_set_array(info
, vers
);
1378 if ((vers
% 100) >= 1) { /* can use different versions */
1379 mdu_array_info_t inf
;
1380 memset(&inf
, 0, sizeof(inf
));
1381 inf
.major_version
= info
->array
.major_version
;
1382 inf
.minor_version
= info
->array
.minor_version
;
1383 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1385 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1389 unsigned long long min_recovery_start(struct mdinfo
*array
)
1391 /* find the minimum recovery_start in an array for metadata
1392 * formats that only record per-array recovery progress instead
1395 unsigned long long recovery_start
= MaxSector
;
1398 for (d
= array
->devs
; d
; d
= d
->next
)
1399 recovery_start
= min(recovery_start
, d
->recovery_start
);
1401 return recovery_start
;
1404 char *devnum2devname(int num
)
1408 sprintf(name
, "md%d", num
);
1410 sprintf(name
, "md_d%d", -1-num
);
1411 return strdup(name
);
1414 int devname2devnum(char *name
)
1418 if (strncmp(name
, "md_d", 4)==0)
1419 num
= -1-strtoul(name
+4, &ep
, 10);
1421 num
= strtoul(name
+2, &ep
, 10);
1425 int stat2devnum(struct stat
*st
)
1432 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1433 if (major(st
->st_rdev
) == MD_MAJOR
)
1434 return minor(st
->st_rdev
);
1435 else if (major(st
->st_rdev
) == get_mdp_major())
1436 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1438 /* must be an extended-minor partition. Look at the
1439 * /sys/dev/block/%d:%d link which must look like
1440 * ../../block/mdXXX/mdXXXpYY
1442 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1443 minor(st
->st_rdev
));
1444 n
= readlink(path
, link
, sizeof(link
)-1);
1448 cp
= strrchr(link
, '/');
1450 cp
= strchr(link
, '/');
1451 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1452 return devname2devnum(cp
+1);
1458 int fd2devnum(int fd
)
1461 if (fstat(fd
, &stb
) == 0)
1462 return stat2devnum(&stb
);
1466 int mdmon_pid(int devnum
)
1472 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1473 fd
= open(path
, O_RDONLY
| O_NOATIME
, 0);
1477 n
= read(fd
, pid
, 9);
1484 int mdmon_running(int devnum
)
1486 int pid
= mdmon_pid(devnum
);
1489 if (kill(pid
, 0) == 0)
1494 int start_mdmon(int devnum
)
1508 if (check_env("MDADM_NO_MDMON"))
1511 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1515 sl
= strrchr(pathbuf
, '/');
1520 strcpy(sl
, "mdmon");
1526 /* FIXME yuk. CLOSE_EXEC?? */
1527 for (i
=3; i
< 100; i
++)
1529 for (i
=0; paths
[i
]; i
++)
1531 execl(paths
[i
], "mdmon",
1532 devnum2devname(devnum
),
1535 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1536 "Array remains readonly\n");
1538 default: /* parent - good */
1539 pid
= wait(&status
);
1540 if (pid
< 0 || status
!= 0)
1546 int check_env(char *name
)
1548 char *val
= getenv(name
);
1550 if (val
&& atoi(val
) == 1)
1556 __u32
random32(void)
1559 int rfd
= open("/dev/urandom", O_RDONLY
);
1560 if (rfd
< 0 || read(rfd
, &rv
, 4) != 4)
1568 int flush_metadata_updates(struct supertype
*st
)
1572 st
->update_tail
= NULL
;
1576 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1580 while (st
->updates
) {
1581 struct metadata_update
*mu
= st
->updates
;
1582 st
->updates
= mu
->next
;
1584 send_message(sfd
, mu
, 0);
1592 st
->update_tail
= NULL
;
1596 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1599 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1605 *st
->update_tail
= mu
;
1606 st
->update_tail
= &mu
->next
;
1608 #endif /* MDASSEMBLE */
1611 /* tinyc doesn't optimize this check in ioctl.h out ... */
1612 unsigned int __invalid_size_argument_for_IOC
= 0;