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 void remove_partitions(int fd
)
190 /* remove partitions from this block devices.
191 * This is used for components added to an array
193 #ifdef BLKPG_DEL_PARTITION
194 struct blkpg_ioctl_arg a
;
195 struct blkpg_partition p
;
197 a
.op
= BLKPG_DEL_PARTITION
;
199 a
.datalen
= sizeof(p
);
201 memset(a
.data
, 0, a
.datalen
);
202 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
203 ioctl(fd
, BLKPG
, &a
);
207 int enough(int level
, int raid_disks
, int layout
, int clean
,
208 char *avail
, int avail_disks
)
213 /* This is the tricky one - we need to check
214 * which actual disks are present.
216 copies
= (layout
&255)* ((layout
>>8) & 255);
219 /* there must be one of the 'copies' form 'first' */
225 first
= (first
+1) % raid_disks
;
230 } while (first
!= 0);
234 return avail_disks
>= 1;
237 return avail_disks
== raid_disks
;
239 return avail_disks
>= 1;
243 return avail_disks
>= raid_disks
-1;
245 return avail_disks
>= raid_disks
;
248 return avail_disks
>= raid_disks
-2;
250 return avail_disks
>= raid_disks
;
256 const int uuid_match_any
[4] = { ~0, ~0, ~0, ~0 };
257 int same_uuid(int a
[4], int b
[4], int swapuuid
)
259 if (memcmp(a
, uuid_match_any
, sizeof(int[4])) == 0 ||
260 memcmp(b
, uuid_match_any
, sizeof(int[4])) == 0)
264 /* parse uuids are hostendian.
265 * uuid's from some superblocks are big-ending
266 * if there is a difference, we need to swap..
268 unsigned char *ac
= (unsigned char *)a
;
269 unsigned char *bc
= (unsigned char *)b
;
271 for (i
=0; i
<16; i
+= 4) {
272 if (ac
[i
+0] != bc
[i
+3] ||
273 ac
[i
+1] != bc
[i
+2] ||
274 ac
[i
+2] != bc
[i
+1] ||
288 void copy_uuid(void *a
, int b
[4], int swapuuid
)
291 /* parse uuids are hostendian.
292 * uuid's from some superblocks are big-ending
293 * if there is a difference, we need to swap..
295 unsigned char *ac
= (unsigned char *)a
;
296 unsigned char *bc
= (unsigned char *)b
;
298 for (i
=0; i
<16; i
+= 4) {
308 char *fname_from_uuid(struct supertype
*st
, struct mdinfo
*info
, char *buf
, char sep
)
316 copy_uuid(uuid
, info
->uuid
, st
->ss
->swapuuid
);
317 for (i
= 0; i
< 4; i
++) {
321 for (j
= 3; j
>= 0; j
--) {
322 sprintf(c
,"%02x", (unsigned char) uuid
[j
+4*i
]);
330 int check_ext2(int fd
, char *name
)
333 * Check for an ext2fs file system.
334 * Superblock is always 1K at 1K offset
336 * s_magic is le16 at 56 == 0xEF53
337 * report mtime - le32 at 44
339 * logblksize - le32 at 24
341 unsigned char sb
[1024];
344 if (lseek(fd
, 1024,0)!= 1024)
346 if (read(fd
, sb
, 1024)!= 1024)
348 if (sb
[56] != 0x53 || sb
[57] != 0xef)
351 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
352 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
353 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
354 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
356 fprintf(stderr
," size=%dK mtime=%s",
357 size
*(1<<bsize
), ctime(&mtime
));
361 int check_reiser(int fd
, char *name
)
364 * superblock is at 64K
366 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
369 unsigned char sb
[1024];
371 if (lseek(fd
, 64*1024, 0) != 64*1024)
373 if (read(fd
, sb
, 1024) != 1024)
375 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
376 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
378 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
379 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
380 fprintf(stderr
, " size = %luK\n", size
*4);
385 int check_raid(int fd
, char *name
)
390 struct supertype
*st
= guess_super(fd
);
393 st
->ss
->load_super(st
, fd
, name
);
394 /* Looks like a raid array .. */
395 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
397 st
->ss
->getinfo_super(st
, &info
);
398 st
->ss
->free_super(st
);
399 crtime
= info
.array
.ctime
;
400 level
= map_num(pers
, info
.array
.level
);
401 if (!level
) level
= "-unknown-";
402 fprintf(stderr
, " level=%s devices=%d ctime=%s",
403 level
, info
.array
.raid_disks
, ctime(&crtime
));
411 for (i
=0; i
<5; i
++) {
413 fprintf(stderr
, "%s%s", mesg
, add
);
415 if (fgets(buf
, 100, stdin
)==NULL
)
417 if (buf
[0]=='y' || buf
[0]=='Y')
419 if (buf
[0]=='n' || buf
[0]=='N')
423 fprintf(stderr
, Name
": assuming 'no'\n");
426 #endif /* MDASSEMBLE */
428 char *map_num(mapping_t
*map
, int num
)
438 int map_name(mapping_t
*map
, char *name
)
441 if (strcmp(map
->name
, name
)==0)
449 int is_standard(char *dev
, int *nump
)
451 /* tests if dev is a "standard" md dev name.
452 * i.e if the last component is "/dNN" or "/mdNN",
453 * where NN is a string of digits
454 * Returns 1 if a partitionable standard,
455 * -1 if non-partitonable,
456 * 0 if not a standard name.
458 char *d
= strrchr(dev
, '/');
463 if (strncmp(d
, "/d",2)==0)
464 d
+= 2, type
=1; /* /dev/md/dN{pM} */
465 else if (strncmp(d
, "/md_d", 5)==0)
466 d
+= 5, type
=1; /* /dev/md_dN{pM} */
467 else if (strncmp(d
, "/md", 3)==0)
468 d
+= 3, type
=-1; /* /dev/mdN */
469 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
470 d
+= 1, type
=-1; /* /dev/md/N */
480 if (nump
) *nump
= num
;
487 * convert a major/minor pair for a block device into a name in /dev, if possible.
488 * On the first call, walk /dev collecting name.
489 * Put them in a simple linked listfor now.
496 int devlist_ready
= 0;
498 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
502 if (S_ISLNK(stb
->st_mode
)) {
503 if (stat(name
, &st
) != 0)
508 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
509 char *n
= strdup(name
);
510 struct devmap
*dm
= malloc(sizeof(*dm
));
511 if (strncmp(n
, "/dev/./", 7)==0)
514 dm
->major
= major(stb
->st_rdev
);
515 dm
->minor
= minor(stb
->st_rdev
);
526 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
528 return add_dev(name
, stb
, flag
, NULL
);
530 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
532 return ftw(path
, add_dev_1
, nopenfd
);
535 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
539 #endif /* HAVE_FTW */
540 #endif /* HAVE_NFTW */
543 * Find a block device with the right major/minor number.
544 * If we find multiple names, choose the shortest.
545 * If we find a name in /dev/md/, we prefer that.
546 * This applies only to names for MD devices.
548 char *map_dev(int major
, int minor
, int create
)
551 char *regular
= NULL
, *preferred
=NULL
;
554 if (major
== 0 && minor
== 0)
558 if (!devlist_ready
) {
562 struct devmap
*d
= devlist
;
567 if (lstat(dev
, &stb
)==0 &&
568 S_ISLNK(stb
.st_mode
))
570 nftw(dev
, add_dev
, 10, FTW_PHYS
);
575 for (p
=devlist
; p
; p
=p
->next
)
576 if (p
->major
== major
&&
578 if (strncmp(p
->name
, "/dev/md/",8) == 0) {
579 if (preferred
== NULL
||
580 strlen(p
->name
) < strlen(preferred
))
583 if (regular
== NULL
||
584 strlen(p
->name
) < strlen(regular
))
588 if (!regular
&& !preferred
&& !did_check
) {
592 if (create
&& !regular
&& !preferred
) {
594 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
598 return preferred
? preferred
: regular
;
601 unsigned long calc_csum(void *super
, int bytes
)
603 unsigned long long newcsum
= 0;
606 unsigned int *superc
= (unsigned int*) super
;
608 for(i
=0; i
<bytes
/4; i
++)
610 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
612 /* The in-kernel checksum calculation is always 16bit on
613 * the alpha, though it is 32 bit on i386...
614 * I wonder what it is elsewhere... (it uses and API in
615 * a way that it shouldn't).
617 csum
= (csum
& 0xffff) + (csum
>> 16);
618 csum
= (csum
& 0xffff) + (csum
>> 16);
624 char *human_size(long long bytes
)
628 /* We convert bytes to either centi-M{ega,ibi}bytes or
629 * centi-G{igi,ibi}bytes, with appropriate rounding,
630 * and then print 1/100th of those as a decimal.
631 * We allow upto 2048Megabytes before converting to
632 * gigabytes, as that shows more precision and isn't
633 * too large a number.
634 * Terrabytes are not yet handled.
637 if (bytes
< 5000*1024)
639 else if (bytes
< 2*1024LL*1024LL*1024LL) {
640 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
641 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
642 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
643 cMiB
/100 , cMiB
% 100,
646 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
647 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
648 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
649 cGiB
/100 , cGiB
% 100,
655 char *human_size_brief(long long bytes
)
659 if (bytes
< 5000*1024)
660 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
661 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
663 else if (bytes
< 2*1024LL*1024LL*1024LL)
664 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
666 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
669 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
671 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
676 void print_r10_layout(int layout
)
678 int near
= layout
& 255;
679 int far
= (layout
>> 8) & 255;
680 int offset
= (layout
&0x10000);
684 printf("%s near=%d", sep
, near
);
688 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
690 printf("NO REDUNDANCY");
694 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
695 int chunksize
, unsigned long long devsize
)
699 case 0: data_disks
= raid_disks
; break;
700 case 1: data_disks
= 1; break;
702 case 5: data_disks
= raid_disks
- 1; break;
703 case 6: data_disks
= raid_disks
- 2; break;
704 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
707 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
708 return data_disks
* devsize
;
711 int get_mdp_major(void)
713 static int mdp_major
= -1;
717 int have_devices
= 0;
722 fl
= fopen("/proc/devices", "r");
725 while ((w
= conf_word(fl
, 1))) {
726 if (have_block
&& strcmp(w
, "devices:")==0)
728 have_block
= (strcmp(w
, "Block")==0);
731 if (have_devices
&& strcmp(w
, "mdp")==0)
732 mdp_major
= last_num
;
739 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
740 char *get_md_name(int dev
)
742 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
743 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
744 static char devname
[50];
750 int mdp
= get_mdp_major();
751 if (mdp
< 0) return NULL
;
752 rdev
= makedev(mdp
, (-1-dev
)<<6);
753 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
754 if (stat(devname
, &stb
) == 0
755 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
756 && (stb
.st_rdev
== rdev
))
759 rdev
= makedev(MD_MAJOR
, dev
);
760 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
761 if (stat(devname
, &stb
) == 0
762 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
763 && (stb
.st_rdev
== rdev
))
766 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
767 if (stat(devname
, &stb
) == 0
768 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
769 && (stb
.st_rdev
== rdev
))
772 dn
= map_dev(major(rdev
), minor(rdev
), 0);
775 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
776 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
780 if (stat(devname
, &stb
) == 0
781 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
782 && (stb
.st_rdev
== rdev
))
788 void put_md_name(char *name
)
790 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
794 int find_free_devnum(int use_partitions
)
797 for (devnum
= 127; devnum
!= 128;
798 devnum
= devnum
? devnum
-1 : (1<<20)-1) {
802 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
803 if (mddev_busy(_devnum
))
805 /* make sure it is new to /dev too, at least as a
807 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
808 if (dn
&& ! is_standard(dn
, NULL
))
814 return use_partitions
? (-1-devnum
) : devnum
;
816 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
818 int dev_open(char *dev
, int flags
)
820 /* like 'open', but if 'dev' matches %d:%d, create a temp
821 * block device and open that
831 major
= strtoul(dev
, &e
, 0);
832 if (e
> dev
&& *e
== ':' && e
[1] &&
833 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
835 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
836 (int)getpid(), major
, minor
);
837 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
838 fd
= open(devname
, flags
|O_DIRECT
);
842 fd
= open(dev
, flags
|O_DIRECT
);
846 int open_dev(int devnum
)
850 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
851 return dev_open(buf
, O_RDWR
);
854 int open_dev_excl(int devnum
)
859 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
860 for (i
=0 ; i
<25 ; i
++) {
861 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
871 int same_dev(char *one
, char *two
)
873 struct stat st1
, st2
;
874 if (stat(one
, &st1
) != 0)
876 if (stat(two
, &st2
) != 0)
878 if ((st1
.st_mode
& S_IFMT
) != S_IFBLK
)
880 if ((st2
.st_mode
& S_IFMT
) != S_IFBLK
)
882 return st1
.st_rdev
== st2
.st_rdev
;
885 void wait_for(char *dev
, int fd
)
888 struct stat stb_want
;
890 if (fstat(fd
, &stb_want
) != 0 ||
891 (stb_want
.st_mode
& S_IFMT
) != S_IFBLK
)
894 for (i
=0 ; i
<25 ; i
++) {
896 if (stat(dev
, &stb
) == 0 &&
897 (stb
.st_mode
& S_IFMT
) == S_IFBLK
&&
898 (stb
.st_rdev
== stb_want
.st_rdev
))
904 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
906 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
908 struct supertype
*super_by_fd(int fd
)
910 mdu_array_info_t array
;
913 struct supertype
*st
= NULL
;
918 char *subarray
= NULL
;
920 sra
= sysfs_read(fd
, 0, GET_VERSION
);
923 vers
= sra
->array
.major_version
;
924 minor
= sra
->array
.minor_version
;
925 verstr
= sra
->text_version
;
927 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
928 array
.major_version
= array
.minor_version
= 0;
929 vers
= array
.major_version
;
930 minor
= array
.minor_version
;
935 sprintf(version
, "%d.%d", vers
, minor
);
938 if (minor
== -2 && is_subarray(verstr
)) {
939 char *dev
= verstr
+1;
940 subarray
= strchr(dev
, '/');
944 devnum
= devname2devnum(dev
);
945 subarray
= strdup(subarray
);
948 sra
= sysfs_read(-1, devnum
, GET_VERSION
);
949 if (sra
&& sra
->text_version
[0])
950 verstr
= sra
->text_version
;
952 verstr
= "-no-metadata-";
955 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
956 st
= superlist
[i
]->match_metadata_desc(verstr
);
963 strncpy(st
->subarray
, subarray
, 32);
964 st
->subarray
[31] = 0;
971 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
974 struct supertype
*dup_super(struct supertype
*orig
)
976 struct supertype
*st
;
980 st
= malloc(sizeof(*st
));
983 memset(st
, 0, sizeof(*st
));
985 st
->max_devs
= orig
->max_devs
;
986 st
->minor_version
= orig
->minor_version
;
987 strcpy(st
->subarray
, orig
->subarray
);
993 struct supertype
*guess_super(int fd
)
995 /* try each load_super to find the best match,
996 * and return the best superswitch
998 struct superswitch
*ss
;
999 struct supertype
*st
;
1000 unsigned long besttime
= 0;
1004 st
= malloc(sizeof(*st
));
1005 for (i
=0 ; superlist
[i
]; i
++) {
1008 memset(st
, 0, sizeof(*st
));
1009 rv
= ss
->load_super(st
, fd
, NULL
);
1012 st
->ss
->getinfo_super(st
, &info
);
1013 if (bestsuper
== -1 ||
1014 besttime
< info
.array
.ctime
) {
1016 besttime
= info
.array
.ctime
;
1021 if (bestsuper
!= -1) {
1023 memset(st
, 0, sizeof(*st
));
1024 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
1026 superlist
[bestsuper
]->free_super(st
);
1034 /* Return size of device in bytes */
1035 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
1037 unsigned long long ldsize
;
1040 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
1041 ldsize
= (unsigned long long)st
.st_size
;
1044 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
1047 unsigned long dsize
;
1048 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
1053 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
1054 dname
, strerror(errno
));
1062 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
1065 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
1066 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
1067 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
1071 int open_container(int fd
)
1073 /* 'fd' is a block device. Find out if it is in use
1074 * by a container, and return an open fd on that container.
1085 if (fstat(fd
, &st
) != 0)
1087 sprintf(path
, "/sys/dev/block/%d:%d/holders",
1088 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
1089 e
= path
+ strlen(path
);
1091 dir
= opendir(path
);
1094 while ((de
= readdir(dir
))) {
1097 if (de
->d_name
[0] == '.')
1099 sprintf(e
, "/%s/dev", de
->d_name
);
1100 dfd
= open(path
, O_RDONLY
);
1103 n
= read(dfd
, buf
, sizeof(buf
));
1105 if (n
<= 0 || n
>= sizeof(buf
))
1108 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
1110 sprintf(buf
, "%d:%d", major
, minor
);
1111 dfd
= dev_open(buf
, O_RDONLY
);
1121 int add_disk(int mdfd
, struct supertype
*st
,
1122 struct mdinfo
*sra
, struct mdinfo
*info
)
1124 /* Add a device to an array, in one of 2 ways. */
1127 if (st
->ss
->external
) {
1128 rv
= sysfs_add_disk(sra
, info
,
1129 info
->disk
.state
& (1<<MD_DISK_SYNC
));
1132 for (sd2
= sra
->devs
; sd2
; sd2
=sd2
->next
)
1136 sd2
= malloc(sizeof(*sd2
));
1138 sd2
->next
= sra
->devs
;
1144 rv
= ioctl(mdfd
, ADD_NEW_DISK
, &info
->disk
);
1148 int set_array_info(int mdfd
, struct supertype
*st
, struct mdinfo
*info
)
1150 /* Initialise kernel's knowledge of array.
1151 * This varies between externally managed arrays
1154 int vers
= md_get_version(mdfd
);
1158 if (st
->ss
->external
)
1159 rv
= sysfs_set_array(info
, vers
);
1162 if ((vers
% 100) >= 1) { /* can use different versions */
1163 mdu_array_info_t inf
;
1164 memset(&inf
, 0, sizeof(inf
));
1165 inf
.major_version
= info
->array
.major_version
;
1166 inf
.minor_version
= info
->array
.minor_version
;
1167 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
1169 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
1173 char *devnum2devname(int num
)
1177 sprintf(name
, "md%d", num
);
1179 sprintf(name
, "md_d%d", -1-num
);
1180 return strdup(name
);
1183 int devname2devnum(char *name
)
1187 if (strncmp(name
, "md_d", 4)==0)
1188 num
= -1-strtoul(name
+4, &ep
, 10);
1190 num
= strtoul(name
+2, &ep
, 10);
1194 int stat2devnum(struct stat
*st
)
1201 if ((S_IFMT
& st
->st_mode
) == S_IFBLK
) {
1202 if (major(st
->st_rdev
) == MD_MAJOR
)
1203 return minor(st
->st_rdev
);
1204 else if (major(st
->st_rdev
) == get_mdp_major())
1205 return -1- (minor(st
->st_rdev
)>>MdpMinorShift
);
1207 /* must be an extended-minor partition. Look at the
1208 * /sys/dev/block/%d:%d link which must look like
1209 * ../../block/mdXXX/mdXXXpYY
1211 sprintf(path
, "/sys/dev/block/%d:%d", major(st
->st_rdev
),
1212 minor(st
->st_rdev
));
1213 n
= readlink(path
, link
, sizeof(link
)-1);
1217 cp
= strrchr(link
, '/');
1219 cp
= strchr(link
, '/');
1220 if (cp
&& strncmp(cp
, "/md", 3) == 0)
1221 return devname2devnum(cp
+1);
1227 int fd2devnum(int fd
)
1230 if (fstat(fd
, &stb
) == 0)
1231 return stat2devnum(&stb
);
1235 int mdmon_running(int devnum
)
1241 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1242 fd
= open(path
, O_RDONLY
, 0);
1246 n
= read(fd
, pid
, 9);
1250 if (kill(atoi(pid
), 0) == 0)
1255 int signal_mdmon(int devnum
)
1261 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1262 fd
= open(path
, O_RDONLY
, 0);
1266 n
= read(fd
, pid
, 9);
1270 if (kill(atoi(pid
), SIGUSR1
) == 0)
1275 int start_mdmon(int devnum
)
1289 if (check_env("MDADM_NO_MDMON"))
1292 len
= readlink("/proc/self/exe", pathbuf
, sizeof(pathbuf
));
1296 sl
= strrchr(pathbuf
, '/');
1301 strcpy(sl
, "mdmon");
1307 /* FIXME yuk. CLOSE_EXEC?? */
1308 for (i
=3; i
< 100; i
++)
1310 for (i
=0; paths
[i
]; i
++)
1312 execl(paths
[i
], "mdmon",
1313 devnum2devname(devnum
),
1316 case -1: fprintf(stderr
, Name
": cannot run mdmon. "
1317 "Array remains readonly\n");
1319 default: /* parent - good */
1320 pid
= wait(&status
);
1321 if (pid
< 0 || status
!= 0)
1327 int check_env(char *name
)
1329 char *val
= getenv(name
);
1331 if (val
&& atoi(val
) == 1)
1338 int flush_metadata_updates(struct supertype
*st
)
1342 st
->update_tail
= NULL
;
1346 sfd
= connect_monitor(devnum2devname(st
->container_dev
));
1350 while (st
->updates
) {
1351 struct metadata_update
*mu
= st
->updates
;
1352 st
->updates
= mu
->next
;
1354 send_message(sfd
, mu
, 0);
1362 st
->update_tail
= NULL
;
1366 void append_metadata_update(struct supertype
*st
, void *buf
, int len
)
1369 struct metadata_update
*mu
= malloc(sizeof(*mu
));
1375 *st
->update_tail
= mu
;
1376 st
->update_tail
= &mu
->next
;
1378 #endif /* MDASSEMBLE */
1381 /* tinyc doesn't optimize this check in ioctl.h out ... */
1382 unsigned int __invalid_size_argument_for_IOC
= 0;