2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
32 #include <sys/socket.h>
33 #include <sys/utsname.h>
41 * following taken from linux/blkpg.h because they aren't
42 * anywhere else and it isn't safe to #include linux/ * stuff.
45 #define BLKPG _IO(0x12,105)
47 /* The argument structure */
48 struct blkpg_ioctl_arg
{
55 /* The subfunctions (for the op field) */
56 #define BLKPG_ADD_PARTITION 1
57 #define BLKPG_DEL_PARTITION 2
59 /* Sizes of name fields. Unused at present. */
60 #define BLKPG_DEVNAMELTH 64
61 #define BLKPG_VOLNAMELTH 64
63 /* The data structure for ADD_PARTITION and DEL_PARTITION */
64 struct blkpg_partition
{
65 long long start
; /* starting offset in bytes */
66 long long length
; /* length in bytes */
67 int pno
; /* partition number */
68 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
69 to be used in kernel messages */
70 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
74 * Parse a 128 bit uuid in 4 integers
75 * format is 32 hexx nibbles with options :.<space> separator
76 * If not exactly 32 hex digits are found, return 0
79 int parse_uuid(char *str
, int uuid
[4])
81 int hit
= 0; /* number of Hex digIT */
84 for (i
=0; i
<4; i
++) uuid
[i
]=0;
90 else if (c
>='a' && c
<= 'f')
92 else if (c
>='A' && c
<= 'F')
94 else if (strchr(":. -", c
))
111 * Get the md version number.
112 * We use the RAID_VERSION ioctl if it is supported
113 * If not, but we have a block device with major '9', we assume
116 * Return version number as 24 but number - assume version parts
120 int md_get_version(int fd
)
125 if (fstat(fd
, &stb
)<0)
127 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
130 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
131 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
134 if (major(stb
.st_rdev
) == MD_MAJOR
)
139 int get_linux_version()
148 a
= strtoul(cp
, &cp
, 10);
149 if (*cp
!= '.') return -1;
150 b
= strtoul(cp
+1, &cp
, 10);
151 if (*cp
!= '.') return -1;
152 c
= strtoul(cp
+1, NULL
, 10);
154 return (a
*1000000)+(b
*1000)+c
;
157 long long parse_size(char *size
)
159 /* parse 'size' which should be a number optionally
160 * followed by 'K', 'M', or 'G'.
161 * Without a suffix, K is assumed.
162 * Number returned is in sectors (half-K)
165 long long s
= strtoll(size
, &c
, 10);
179 s
*= 1024 * 1024 * 2;
188 int parse_layout_10(char *layout
)
192 /* Parse the layout string for raid10 */
193 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
194 if ((layout
[0] != 'n' && layout
[0] != 'f' && layout
[0] != 'o') ||
195 (copies
= strtoul(layout
+1, &cp
, 10)) < 1 ||
199 if (layout
[0] == 'n')
201 else if (layout
[0] == 'o')
202 rv
= 0x10000 + (copies
<<8) + 1;
204 rv
= 1 + (copies
<<8);
208 int parse_layout_faulty(char *layout
)
210 /* Parse the layout string for 'faulty' */
211 int ln
= strcspn(layout
, "0123456789");
212 char *m
= strdup(layout
);
215 mode
= map_name(faultylayout
, m
);
219 return mode
| (atoi(layout
+ln
)<< ModeShift
);
222 void remove_partitions(int fd
)
224 /* remove partitions from this block devices.
225 * This is used for components added to an array
227 #ifdef BLKPG_DEL_PARTITION
228 struct blkpg_ioctl_arg a
;
229 struct blkpg_partition p
;
231 a
.op
= BLKPG_DEL_PARTITION
;
233 a
.datalen
= sizeof(p
);
235 memset(a
.data
, 0, a
.datalen
);
236 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
237 ioctl(fd
, BLKPG
, &a
);
241 int enough(int level
, int raid_disks
, int layout
, int clean
,
242 char *avail
, int avail_disks
)
247 /* This is the tricky one - we need to check
248 * which actual disks are present.
250 copies
= (layout
&255)* ((layout
>>8) & 255);
253 /* there must be one of the 'copies' form 'first' */
259 first
= (first
+1) % raid_disks
;
264 } while (first
!= 0);
268 return avail_disks
>= 1;
271 return avail_disks
== raid_disks
;
273 return avail_disks
>= 1;
277 return avail_disks
>= raid_disks
-1;
279 return avail_disks
>= raid_disks
;
282 return avail_disks
>= raid_disks
-2;
284 return avail_disks
>= raid_disks
;
290 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
291 int same_uuid(int a
[4], int b
[4], int swapuuid
)
293 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
294 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
298 /* parse uuids are hostendian.
299 * uuid's from some superblocks are big-ending
300 * if there is a difference, we need to swap..
302 unsigned char *ac
= (unsigned char *)a
;
303 unsigned char *bc
= (unsigned char *)b
;
305 for (i
=0; i
<16; i
+= 4) {
306 if (ac
[i
+0] != bc
[i
+3] ||
307 ac
[i
+1] != bc
[i
+2] ||
308 ac
[i
+2] != bc
[i
+1] ||
322 void copy_uuid(void *a
, int b
[4], int swapuuid
)
325 /* parse uuids are hostendian.
326 * uuid's from some superblocks are big-ending
327 * if there is a difference, we need to swap..
329 unsigned char *ac
= (unsigned char *)a
;
330 unsigned char *bc
= (unsigned char *)b
;
332 for (i
=0; i
<16; i
+= 4) {
342 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
350 copy_uuid(uuid
, info
->uuid
, st
->ss
->swapuuid
);
351 for (i
= 0; i
< 4; i
++) {
355 for (j
= 3; j
>= 0; j
--) {
356 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
364 int check_ext2(int fd
, char *name
)
367 * Check for an ext2fs file system.
368 * Superblock is always 1K at 1K offset
370 * s_magic is le16 at 56 == 0xEF53
371 * report mtime - le32 at 44
373 * logblksize - le32 at 24
375 unsigned char sb
[1024];
378 if (lseek(fd
, 1024,0)!= 1024)
380 if (read(fd
, sb
, 1024)!= 1024)
382 if (sb
[56] != 0x53 || sb
[57] != 0xef)
385 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
386 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
387 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
388 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
390 fprintf(stderr
," size=%dK mtime=%s",
391 size
*(1<<bsize
), ctime(&mtime
));
395 int check_reiser(int fd
, char *name
)
398 * superblock is at 64K
400 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
403 unsigned char sb
[1024];
405 if (lseek(fd
, 64*1024, 0) != 64*1024)
407 if (read(fd
, sb
, 1024) != 1024)
409 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
410 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
412 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
413 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
414 fprintf(stderr
, " size = %luK\n", size
*4);
419 int check_raid(int fd
, char *name
)
424 struct supertype
*st
= guess_super(fd
);
427 st
->ss
->load_super(st
, fd
, name
);
428 /* Looks like a raid array .. */
429 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
431 st
->ss
->getinfo_super(st
, &info
);
432 st
->ss
->free_super(st
);
433 crtime
= info
.array
.ctime
;
434 level
= map_num(pers
, info
.array
.level
);
435 if (!level
) level
= "-unknown-";
436 fprintf(stderr
, " level=%s devices=%d ctime=%s",
437 level
, info
.array
.raid_disks
, ctime(&crtime
));
445 for (i
=0; i
<5; i
++) {
447 fprintf(stderr
, "%s%s", mesg
, add
);
449 if (fgets(buf
, 100, stdin
)==NULL
)
451 if (buf
[0]=='y' || buf
[0]=='Y')
453 if (buf
[0]=='n' || buf
[0]=='N')
457 fprintf(stderr
, Name
": assuming 'no'\n");
460 #endif /* MDASSEMBLE */
462 char *map_num(mapping_t
*map
, int num
)
472 int map_name(mapping_t
*map
, char *name
)
475 if (strcmp(map
->name
, name
)==0)
483 int is_standard(char *dev
, int *nump
)
485 /* tests if dev is a "standard" md dev name.
486 * i.e if the last component is "/dNN" or "/mdNN",
487 * where NN is a string of digits
488 * Returns 1 if a partitionable standard,
489 * -1 if non-partitonable,
490 * 0 if not a standard name.
492 char *d
= strrchr(dev
, '/');
497 if (strncmp(d
, "/d",2)==0)
498 d
+= 2, type
=1; /* /dev/md/dN{pM} */
499 else if (strncmp(d
, "/md_d", 5)==0)
500 d
+= 5, type
=1; /* /dev/md_dN{pM} */
501 else if (strncmp(d
, "/md", 3)==0)
502 d
+= 3, type
=-1; /* /dev/mdN */
503 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
504 d
+= 1, type
=-1; /* /dev/md/N */
514 if (nump
) *nump
= num
;
521 * convert a major/minor pair for a block device into a name in /dev, if possible.
522 * On the first call, walk /dev collecting name.
523 * Put them in a simple linked listfor now.
530 int devlist_ready
= 0;
532 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
536 if (S_ISLNK(stb
->st_mode
)) {
537 if (stat(name
, &st
) != 0)
542 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
543 char *n
= strdup(name
);
544 struct devmap
*dm
= malloc(sizeof(*dm
));
545 if (strncmp(n
, "/dev/./", 7)==0)
548 dm
->major
= major(stb
->st_rdev
);
549 dm
->minor
= minor(stb
->st_rdev
);
560 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
562 return add_dev(name
, stb
, flag
, NULL
);
564 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
566 return ftw(path
, add_dev_1
, nopenfd
);
569 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
573 #endif /* HAVE_FTW */
574 #endif /* HAVE_NFTW */
577 * Find a block device with the right major/minor number.
578 * If we find multiple names, choose the shortest.
579 * If we find a name in /dev/md/, we prefer that.
580 * This applies only to names for MD devices.
582 char *map_dev(int major
, int minor
, int create
)
585 char *regular
= NULL
, *preferred
=NULL
;
588 if (major
== 0 && minor
== 0)
592 if (!devlist_ready
) {
596 struct devmap
*d
= devlist
;
601 if (lstat(dev
, &stb
)==0 &&
602 S_ISLNK(stb
.st_mode
))
604 nftw(dev
, add_dev
, 10, FTW_PHYS
);
609 for (p
=devlist
; p
; p
=p
->next
)
610 if (p
->major
== major
&&
612 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
613 if (preferred
== NULL
||
614 strlen(p
->name
) < strlen(preferred
))
617 if (regular
== NULL
||
618 strlen(p
->name
) < strlen(regular
))
622 if (!regular
&& !preferred
&& !did_check
) {
626 if (create
&& !regular
&& !preferred
) {
628 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
632 return preferred
? preferred
: regular
;
635 unsigned long calc_csum(void *super
, int bytes
)
637 unsigned long long newcsum
= 0;
640 unsigned int *superc
= (unsigned int*) super
;
642 for(i
=0; i
<bytes
/4; i
++)
644 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
646 /* The in-kernel checksum calculation is always 16bit on
647 * the alpha, though it is 32 bit on i386...
648 * I wonder what it is elsewhere... (it uses and API in
649 * a way that it shouldn't).
651 csum
= (csum
& 0xffff) + (csum
>> 16);
652 csum
= (csum
& 0xffff) + (csum
>> 16);
658 char *human_size(long long bytes
)
662 /* We convert bytes to either centi-M{ega,ibi}bytes or
663 * centi-G{igi,ibi}bytes, with appropriate rounding,
664 * and then print 1/100th of those as a decimal.
665 * We allow upto 2048Megabytes before converting to
666 * gigabytes, as that shows more precision and isn't
667 * too large a number.
668 * Terrabytes are not yet handled.
671 if (bytes
< 5000*1024)
673 else if (bytes
< 2*1024LL*1024LL*1024LL) {
674 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
675 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
676 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
677 cMiB
/100 , cMiB
% 100,
680 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
681 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
682 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
683 cGiB
/100 , cGiB
% 100,
689 char *human_size_brief(long long bytes
)
693 if (bytes
< 5000*1024)
694 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
695 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
697 else if (bytes
< 2*1024LL*1024LL*1024LL)
698 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
700 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
703 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
705 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
710 void print_r10_layout(int layout
)
712 int near
= layout
& 255;
713 int far
= (layout
>> 8) & 255;
714 int offset
= (layout
&0x10000);
718 printf("%s near=%d", sep
, near
);
722 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
724 printf("NO REDUNDANCY");
728 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
729 int chunksize
, unsigned long long devsize
)
733 case 0: data_disks
= raid_disks
; break;
734 case 1: data_disks
= 1; break;
736 case 5: data_disks
= raid_disks
- 1; break;
737 case 6: data_disks
= raid_disks
- 2; break;
738 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
741 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
742 return data_disks
* devsize
;
745 int get_mdp_major(void)
747 static int mdp_major
= -1;
751 int have_devices
= 0;
756 fl
= fopen("/proc/devices", "r");
759 while ((w
= conf_word(fl
, 1))) {
760 if (have_block
&& strcmp(w
, "devices:")==0)
762 have_block
= (strcmp(w
, "Block")==0);
765 if (have_devices
&& strcmp(w
, "mdp")==0)
766 mdp_major
= last_num
;
773 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
774 char *get_md_name(int dev
)
776 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
777 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
778 static char devname
[50];
784 int mdp
= get_mdp_major();
785 if (mdp
< 0) return NULL
;
786 rdev
= makedev(mdp
, (-1-dev
)<<6);
787 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
788 if (stat(devname
, &stb
) == 0
789 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
790 && (stb
.st_rdev
== rdev
))
793 rdev
= makedev(MD_MAJOR
, dev
);
794 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
795 if (stat(devname
, &stb
) == 0
796 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
797 && (stb
.st_rdev
== rdev
))
800 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
801 if (stat(devname
, &stb
) == 0
802 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
803 && (stb
.st_rdev
== rdev
))
806 dn
= map_dev(major(rdev
), minor(rdev
), 0);
809 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
810 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
814 if (stat(devname
, &stb
) == 0
815 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
816 && (stb
.st_rdev
== rdev
))
822 void put_md_name(char *name
)
824 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
828 int find_free_devnum(int use_partitions
)
831 for (devnum
= 127; devnum
!= 128;
832 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
836 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
837 if (mddev_busy(_devnum
))
839 /* make sure it is new to /dev too, at least as a
841 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
842 if (dn
&& ! is_standard(dn
, NULL
))
848 return use_partitions
? (-1-devnum
) : devnum
;
850 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
852 int dev_open(char *dev
, int flags
)
854 /* like 'open', but if 'dev' matches %d:%d, create a temp
855 * block device and open that
865 major
= strtoul(dev
, &e
, 0);
866 if (e
> dev
&& *e
== ':' && e
[1] &&
867 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
869 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
870 (int)getpid(), major
, minor
);
871 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
872 fd
= open(devname
, flags
|O_DIRECT
);
876 fd
= open(dev
, flags
|O_DIRECT
);
880 int open_dev(int devnum
)
884 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
885 return dev_open(buf
, O_RDWR
);
888 int open_dev_excl(int devnum
)
893 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
894 for (i
=0 ; i
<25 ; i
++) {
895 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
905 int same_dev(char *one
, char *two
)
907 struct stat st1
, st2
;
908 if (stat(one
, &st1
) != 0)
910 if (stat(two
, &st2
) != 0)
912 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
914 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
916 return st1
.st_rdev
== st2
.st_rdev
;
919 void wait_for(char *dev
, int fd
)
922 struct stat stb_want
;
924 if (fstat(fd
, &stb_want
) != 0 ||
925 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
928 for (i
=0 ; i
<25 ; i
++) {
930 if (stat(dev
, &stb
) == 0 &&
931 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
932 (stb
.st_rdev
== stb_want
.st_rdev
))
938 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
940 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
942 struct supertype
*super_by_fd(int fd
)
944 mdu_array_info_t array
;
947 struct supertype
*st
= NULL
;
952 char *subarray
= NULL
;
954 sra
= sysfs_read(fd
, 0, GET_VERSION
);
957 vers
= sra
->array
.major_version
;
958 minor
= sra
->array
.minor_version
;
959 verstr
= sra
->text_version
;
961 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
962 array
.major_version
= array
.minor_version
= 0;
963 vers
= array
.major_version
;
964 minor
= array
.minor_version
;
969 sprintf(version
, "%d.%d", vers
, minor
);
972 if (minor
== -2 && is_subarray(verstr
)) {
973 char *dev
= verstr
+1;
974 subarray
= strchr(dev
, '/');
978 devnum
= devname2devnum(dev
);
979 subarray
= strdup(subarray
);
982 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
983 if (sra
&& sra
->text_version
[0])
984 verstr
= sra
->text_version
;
986 verstr
= "-no-metadata-";
989 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
990 st
= superlist
[i
]->match_metadata_desc(verstr
);
997 strncpy(st
->subarray
, subarray
, 32);
998 st
->subarray
[31] = 0;
1001 st
->subarray
[0] = 0;
1005 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
1008 struct supertype
*dup_super(struct supertype
*orig
)
1010 struct supertype
*st
;
1014 st
= malloc(sizeof(*st
));
1017 memset(st
, 0, sizeof(*st
));
1019 st
->max_devs
= orig
->max_devs
;
1020 st
->minor_version
= orig
->minor_version
;
1021 strcpy(st
->subarray
, orig
->subarray
);
1027 struct supertype
*guess_super(int fd
)
1029 /* try each load_super to find the best match,
1030 * and return the best superswitch
1032 struct superswitch
*ss
;
1033 struct supertype
*st
;
1034 unsigned long besttime
= 0;
1038 st
= malloc(sizeof(*st
));
1039 for (i
=0 ; superlist
[i
]; i
++) {
1042 memset(st
, 0, sizeof(*st
));
1043 rv
= ss
->load_super(st
, fd
, NULL
);
1046 st
->ss
->getinfo_super(st
, &info
);
1047 if (bestsuper
== -1 ||
1048 besttime
< info
.array
.ctime
) {
1050 besttime
= info
.array
.ctime
;
1055 if (bestsuper
!= -1) {
1057 memset(st
, 0, sizeof(*st
));
1058 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1060 superlist
[bestsuper
]->free_super(st
);
1068 /* Return size of device in bytes */
1069 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1071 unsigned long long ldsize
;
1074 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1075 ldsize
= (unsigned long long)st
.st_size
;
1078 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1081 unsigned long dsize
;
1082 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1087 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1088 dname
, strerror(errno
));
1096 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1099 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1100 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1101 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1105 int open_container(int fd
)
1107 /* 'fd' is a block device. Find out if it is in use
1108 * by a container, and return an open fd on that container.
1119 if (fstat(fd
, &st
) != 0)
1121 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1122 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1123 e
= path
+ strlen(path
);
1125 dir
= opendir(path
);
1128 while ((de
= readdir(dir
))) {
1131 if (de
->d_name
[0] == '.')
1133 sprintf(e
, "/%s/dev", de
->d_name
);
1134 dfd
= open(path
, O_RDONLY
);
1137 n
= read(dfd
, buf
, sizeof(buf
));
1139 if (n
<= 0 || n
>= sizeof(buf
))
1142 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1144 sprintf(buf
, "%d:%d", major
, minor
);
1145 dfd
= dev_open(buf
, O_RDONLY
);
1155 int add_disk(int mdfd
, struct supertype
*st
,
1156 struct mdinfo
*sra
, struct mdinfo
*info
)
1158 /* Add a device to an array, in one of 2 ways. */
1161 if (st
->ss
->external
) {
1162 rv
= sysfs_add_disk(sra
, info
,
1163 info
->disk
.state
& (1<<MD_DISK_SYNC
));
1166 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1170 sd2
= malloc(sizeof(*sd2
));
1172 sd2
->next
= sra
->devs
;
1178 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1182 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1184 /* Initialise kernel's knowledge of array.
1185 * This varies between externally managed arrays
1188 int vers
= md_get_version(mdfd
);
1192 if (st
->ss
->external
)
1193 rv
= sysfs_set_array(info
, vers
);
1196 if ((vers
% 100) >= 1) { /* can use different versions */
1197 mdu_array_info_t inf
;
1198 memset(&inf
, 0, sizeof(inf
));
1199 inf
.major_version
= info
->array
.major_version
;
1200 inf
.minor_version
= info
->array
.minor_version
;
1201 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1203 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1207 char *devnum2devname(int num
)
1211 sprintf(name
, "md%d", num
);
1213 sprintf(name
, "md_d%d", -1-num
);
1214 return strdup(name
);
1217 int devname2devnum(char *name
)
1221 if (strncmp(name
, "md_d", 4)==0)
1222 num
= -1-strtoul(name
+4, &ep
, 10);
1224 num
= strtoul(name
+2, &ep
, 10);
1228 int stat2devnum(struct stat
*st
)
1235 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1236 if (major(st
->st_rdev
) == MD_MAJOR
)
1237 return minor(st
->st_rdev
);
1238 else if (major(st
->st_rdev
) == get_mdp_major())
1239 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1241 /* must be an extended-minor partition. Look at the
1242 * /sys/dev/block/%d:%d link which must look like
1243 * ../../block/mdXXX/mdXXXpYY
1245 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1246 minor(st
->st_rdev
));
1247 n
= readlink(path
, link
, sizeof(link
)-1);
1251 cp
= strrchr(link
, '/');
1253 cp
= strchr(link
, '/');
1254 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1255 return devname2devnum(cp
+1);
1261 int fd2devnum(int fd
)
1264 if (fstat(fd
, &stb
) == 0)
1265 return stat2devnum(&stb
);
1269 int mdmon_running(int devnum
)
1275 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1276 fd
= open(path
, O_RDONLY
, 0);
1280 n
= read(fd
, pid
, 9);
1284 if (kill(atoi(pid
), 0) == 0)
1289 int signal_mdmon(int devnum
)
1295 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1296 fd
= open(path
, O_RDONLY
, 0);
1300 n
= read(fd
, pid
, 9);
1304 if (kill(atoi(pid
), SIGUSR1
) == 0)
1309 int start_mdmon(int devnum
)
1323 if (check_env("MDADM_NO_MDMON"))
1326 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1330 sl
= strrchr(pathbuf
, '/');
1335 strcpy(sl
, "mdmon");
1341 /* FIXME yuk. CLOSE_EXEC?? */
1342 for (i
=3; i
< 100; i
++)
1344 for (i
=0; paths
[i
]; i
++)
1346 execl(paths
[i
], "mdmon",
1347 devnum2devname(devnum
),
1350 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1351 "Array remains readonly\n");
1353 default: /* parent - good */
1354 pid
= wait(&status
);
1355 if (pid
< 0 || status
!= 0)
1361 int check_env(char *name
)
1363 char *val
= getenv(name
);
1365 if (val
&& atoi(val
) == 1)
1372 int flush_metadata_updates(struct supertype
*st
)
1376 st
->update_tail
= NULL
;
1380 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1384 while (st
->updates
) {
1385 struct metadata_update
*mu
= st
->updates
;
1386 st
->updates
= mu
->next
;
1388 send_message(sfd
, mu
, 0);
1396 st
->update_tail
= NULL
;
1400 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1403 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1409 *st
->update_tail
= mu
;
1410 st
->update_tail
= &mu
->next
;
1412 #endif /* MDASSEMBLE */
1415 /* tinyc doesn't optimize this check in ioctl.h out ... */
1416 unsigned int __invalid_size_argument_for_IOC
= 0;