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/utsname.h>
38 * following taken from linux/blkpg.h because they aren't
39 * anywhere else and it isn't safe to #include linux/ * stuff.
42 #define BLKPG _IO(0x12,105)
44 /* The argument structure */
45 struct blkpg_ioctl_arg
{
52 /* The subfunctions (for the op field) */
53 #define BLKPG_ADD_PARTITION 1
54 #define BLKPG_DEL_PARTITION 2
56 /* Sizes of name fields. Unused at present. */
57 #define BLKPG_DEVNAMELTH 64
58 #define BLKPG_VOLNAMELTH 64
60 /* The data structure for ADD_PARTITION and DEL_PARTITION */
61 struct blkpg_partition
{
62 long long start
; /* starting offset in bytes */
63 long long length
; /* length in bytes */
64 int pno
; /* partition number */
65 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
66 to be used in kernel messages */
67 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
71 * Parse a 128 bit uuid in 4 integers
72 * format is 32 hexx nibbles with options :.<space> separator
73 * If not exactly 32 hex digits are found, return 0
76 int parse_uuid(char *str
, int uuid
[4])
78 int hit
= 0; /* number of Hex digIT */
81 for (i
=0; i
<4; i
++) uuid
[i
]=0;
87 else if (c
>='a' && c
<= 'f')
89 else if (c
>='A' && c
<= 'F')
91 else if (strchr(":. -", c
))
108 * Get the md version number.
109 * We use the RAID_VERSION ioctl if it is supported
110 * If not, but we have a block device with major '9', we assume
113 * Return version number as 24 but number - assume version parts
117 int md_get_version(int fd
)
122 if (fstat(fd
, &stb
)<0)
124 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
127 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
128 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
131 if (major(stb
.st_rdev
) == MD_MAJOR
)
136 int get_linux_version()
145 a
= strtoul(cp
, &cp
, 10);
146 if (*cp
!= '.') return -1;
147 b
= strtoul(cp
+1, &cp
, 10);
148 if (*cp
!= '.') return -1;
149 c
= strtoul(cp
+1, NULL
, 10);
151 return (a
*1000000)+(b
*1000)+c
;
154 void remove_partitions(int fd
)
156 /* remove partitions from this block devices.
157 * This is used for components added to an array
159 #ifdef BLKPG_DEL_PARTITION
160 struct blkpg_ioctl_arg a
;
161 struct blkpg_partition p
;
163 a
.op
= BLKPG_DEL_PARTITION
;
165 a
.datalen
= sizeof(p
);
167 memset(a
.data
, 0, a
.datalen
);
168 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
169 ioctl(fd
, BLKPG
, &a
);
173 int enough(int level
, int raid_disks
, int layout
, int clean
,
174 char *avail
, int avail_disks
)
179 /* This is the tricky one - we need to check
180 * which actual disks are present.
182 copies
= (layout
&255)* ((layout
>>8) & 255);
185 /* there must be one of the 'copies' form 'first' */
191 first
= (first
+1) % raid_disks
;
196 } while (first
!= 0);
200 return avail_disks
>= 1;
203 return avail_disks
== raid_disks
;
205 return avail_disks
>= 1;
209 return avail_disks
>= raid_disks
-1;
211 return avail_disks
>= raid_disks
;
214 return avail_disks
>= raid_disks
-2;
216 return avail_disks
>= raid_disks
;
222 int same_uuid(int a
[4], int b
[4], int swapuuid
)
225 /* parse uuids are hostendian.
226 * uuid's from some superblocks are big-ending
227 * if there is a difference, we need to swap..
229 unsigned char *ac
= (unsigned char *)a
;
230 unsigned char *bc
= (unsigned char *)b
;
232 for (i
=0; i
<16; i
+= 4) {
233 if (ac
[i
+0] != bc
[i
+3] ||
234 ac
[i
+1] != bc
[i
+2] ||
235 ac
[i
+2] != bc
[i
+1] ||
249 void copy_uuid(void *a
, int b
[4], int swapuuid
)
252 /* parse uuids are hostendian.
253 * uuid's from some superblocks are big-ending
254 * if there is a difference, we need to swap..
256 unsigned char *ac
= (unsigned char *)a
;
257 unsigned char *bc
= (unsigned char *)b
;
259 for (i
=0; i
<16; i
+= 4) {
270 int check_ext2(int fd
, char *name
)
273 * Check for an ext2fs file system.
274 * Superblock is always 1K at 1K offset
276 * s_magic is le16 at 56 == 0xEF53
277 * report mtime - le32 at 44
279 * logblksize - le32 at 24
281 unsigned char sb
[1024];
284 if (lseek(fd
, 1024,0)!= 1024)
286 if (read(fd
, sb
, 1024)!= 1024)
288 if (sb
[56] != 0x53 || sb
[57] != 0xef)
291 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
292 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
293 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
294 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
296 fprintf(stderr
," size=%dK mtime=%s",
297 size
*(1<<bsize
), ctime(&mtime
));
301 int check_reiser(int fd
, char *name
)
304 * superblock is at 64K
306 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
309 unsigned char sb
[1024];
311 if (lseek(fd
, 64*1024, 0) != 64*1024)
313 if (read(fd
, sb
, 1024) != 1024)
315 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
316 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
318 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
319 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
320 fprintf(stderr
, " size = %luK\n", size
*4);
325 int check_raid(int fd
, char *name
)
330 struct supertype
*st
= guess_super(fd
);
333 st
->ss
->load_super(st
, fd
, name
);
334 /* Looks like a raid array .. */
335 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
337 st
->ss
->getinfo_super(st
, &info
);
338 st
->ss
->free_super(st
);
339 crtime
= info
.array
.ctime
;
340 level
= map_num(pers
, info
.array
.level
);
341 if (!level
) level
= "-unknown-";
342 fprintf(stderr
, " level=%s devices=%d ctime=%s",
343 level
, info
.array
.raid_disks
, ctime(&crtime
));
351 for (i
=0; i
<5; i
++) {
353 fprintf(stderr
, "%s%s", mesg
, add
);
355 if (fgets(buf
, 100, stdin
)==NULL
)
357 if (buf
[0]=='y' || buf
[0]=='Y')
359 if (buf
[0]=='n' || buf
[0]=='N')
363 fprintf(stderr
, Name
": assuming 'no'\n");
366 #endif /* MDASSEMBLE */
368 char *map_num(mapping_t
*map
, int num
)
378 int map_name(mapping_t
*map
, char *name
)
381 if (strcmp(map
->name
, name
)==0)
389 int is_standard(char *dev
, int *nump
)
391 /* tests if dev is a "standard" md dev name.
392 * i.e if the last component is "/dNN" or "/mdNN",
393 * where NN is a string of digits
394 * Returns 1 if a partitionable standard,
395 * -1 if non-partitonable,
396 * 0 if not a standard name.
398 char *d
= strrchr(dev
, '/');
403 if (strncmp(d
, "/d",2)==0)
404 d
+= 2, type
=1; /* /dev/md/dN{pM} */
405 else if (strncmp(d
, "/md_d", 5)==0)
406 d
+= 5, type
=1; /* /dev/md_dNpM */
407 else if (strncmp(d
, "/md", 3)==0)
408 d
+= 3, type
=-1; /* /dev/mdN */
409 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
410 d
+= 1, type
=-1; /* /dev/md/N */
420 if (nump
) *nump
= num
;
427 * convert a major/minor pair for a block device into a name in /dev, if possible.
428 * On the first call, walk /dev collecting name.
429 * Put them in a simple linked listfor now.
436 int devlist_ready
= 0;
438 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
441 if (S_ISLNK(stb
->st_mode
)) {
446 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
447 char *n
= strdup(name
);
448 struct devmap
*dm
= malloc(sizeof(*dm
));
449 if (strncmp(n
, "/dev/./", 7)==0)
452 dm
->major
= major(stb
->st_rdev
);
453 dm
->minor
= minor(stb
->st_rdev
);
464 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
466 return add_dev(name
, stb
, flag
, NULL
);
468 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
470 return ftw(path
, add_dev_1
, nopenfd
);
473 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
477 #endif /* HAVE_FTW */
478 #endif /* HAVE_NFTW */
481 * Find a block device with the right major/minor number.
482 * If we find multiple names, choose the shortest.
483 * If we find a non-standard name, it is probably there
484 * deliberately so prefer it over a standard name.
485 * This applies only to names for MD devices.
487 char *map_dev(int major
, int minor
, int create
)
490 char *std
= NULL
, *nonstd
=NULL
;
493 if (major
== 0 && minor
== 0)
497 if (!devlist_ready
) {
501 struct devmap
*d
= devlist
;
506 if (lstat(dev
, &stb
)==0 &&
507 S_ISLNK(stb
.st_mode
))
509 nftw(dev
, add_dev
, 10, FTW_PHYS
);
514 for (p
=devlist
; p
; p
=p
->next
)
515 if (p
->major
== major
&&
517 if (is_standard(p
->name
, NULL
)) {
519 strlen(p
->name
) < strlen(std
))
522 if (nonstd
== NULL
||
523 strlen(p
->name
) < strlen(nonstd
))
527 if (!std
&& !nonstd
&& !did_check
) {
531 if (create
&& !std
&& !nonstd
) {
533 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
537 return nonstd
? nonstd
: std
;
540 unsigned long calc_csum(void *super
, int bytes
)
542 unsigned long long newcsum
= 0;
545 unsigned int *superc
= (unsigned int*) super
;
547 for(i
=0; i
<bytes
/4; i
++)
549 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
551 /* The in-kernel checksum calculation is always 16bit on
552 * the alpha, though it is 32 bit on i386...
553 * I wonder what it is elsewhere... (it uses and API in
554 * a way that it shouldn't).
556 csum
= (csum
& 0xffff) + (csum
>> 16);
557 csum
= (csum
& 0xffff) + (csum
>> 16);
563 char *human_size(long long bytes
)
567 /* We convert bytes to either centi-M{ega,ibi}bytes or
568 * centi-G{igi,ibi}bytes, with appropriate rounding,
569 * and then print 1/100th of those as a decimal.
570 * We allow upto 2048Megabytes before converting to
571 * gigabytes, as that shows more precision and isn't
572 * too large a number.
573 * Terrabytes are not yet handled.
576 if (bytes
< 5000*1024)
578 else if (bytes
< 2*1024LL*1024LL*1024LL) {
579 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
580 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
581 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
582 cMiB
/100 , cMiB
% 100,
585 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
586 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
587 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
588 cGiB
/100 , cGiB
% 100,
594 char *human_size_brief(long long bytes
)
598 if (bytes
< 5000*1024)
599 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
600 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
602 else if (bytes
< 2*1024LL*1024LL*1024LL)
603 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
605 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
608 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
610 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
616 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
617 int chunksize
, unsigned long long devsize
)
621 case 0: data_disks
= raid_disks
; break;
622 case 1: data_disks
= 1; break;
624 case 5: data_disks
= raid_disks
- 1; break;
625 case 6: data_disks
= raid_disks
- 2; break;
626 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
629 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
630 return data_disks
* devsize
;
633 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
634 int get_mdp_major(void)
636 static int mdp_major
= -1;
640 int have_devices
= 0;
645 fl
= fopen("/proc/devices", "r");
648 while ((w
= conf_word(fl
, 1))) {
649 if (have_block
&& strcmp(w
, "devices:")==0)
651 have_block
= (strcmp(w
, "Block")==0);
654 if (have_devices
&& strcmp(w
, "mdp")==0)
655 mdp_major
= last_num
;
664 char *get_md_name(int dev
)
666 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
667 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
668 static char devname
[50];
674 int mdp
= get_mdp_major();
675 if (mdp
< 0) return NULL
;
676 rdev
= makedev(mdp
, (-1-dev
)<<6);
677 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
678 if (stat(devname
, &stb
) == 0
679 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
680 && (stb
.st_rdev
== rdev
))
683 rdev
= makedev(MD_MAJOR
, dev
);
684 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
685 if (stat(devname
, &stb
) == 0
686 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
687 && (stb
.st_rdev
== rdev
))
690 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
691 if (stat(devname
, &stb
) == 0
692 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
693 && (stb
.st_rdev
== rdev
))
696 dn
= map_dev(major(rdev
), minor(rdev
), 0);
699 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
700 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
704 if (stat(devname
, &stb
) == 0
705 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
706 && (stb
.st_rdev
== rdev
))
712 void put_md_name(char *name
)
714 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
718 int find_free_devnum(int use_partitions
)
721 for (devnum
= 127; devnum
!= 128;
722 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
726 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
727 if (mddev_busy(_devnum
))
729 /* make sure it is new to /dev too, at least as a
731 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
732 if (dn
&& ! is_standard(dn
, NULL
))
738 return use_partitions
? (-1-devnum
) : devnum
;
740 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
742 int dev_open(char *dev
, int flags
)
744 /* like 'open', but if 'dev' matches %d:%d, create a temp
745 * block device and open that
755 major
= strtoul(dev
, &e
, 0);
756 if (e
> dev
&& *e
== ':' && e
[1] &&
757 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
759 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d:%d",
760 (int)getpid(), major
, minor
);
761 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
762 fd
= open(devname
, flags
);
766 fd
= open(dev
, flags
);
770 int open_dev_excl(int devnum
)
775 sprintf(buf
, "%d:%d", dev2major(devnum
), dev2minor(devnum
));
776 for (i
=0 ; i
<25 ; i
++) {
777 int fd
= dev_open(buf
, O_RDWR
|O_EXCL
);
787 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
789 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
791 struct supertype supertype_container_member
;
793 struct supertype
*super_by_fd(int fd
)
795 mdu_array_info_t array
;
798 struct supertype
*st
= NULL
;
804 sra
= sysfs_read(fd
, 0, GET_VERSION
);
807 vers
= sra
->array
.major_version
;
808 minor
= sra
->array
.minor_version
;
809 verstr
= sra
->text_version
;
811 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
812 array
.major_version
= array
.minor_version
= 0;
813 vers
= array
.major_version
;
814 minor
= array
.minor_version
;
819 sprintf(version
, "%d.%d", vers
, minor
);
822 if (minor
== -2 && verstr
[0] == '/')
823 st
= &supertype_container_member
;
825 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
826 st
= superlist
[i
]->match_metadata_desc(verstr
);
834 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
837 struct supertype
*dup_super(struct supertype
*orig
)
839 struct supertype
*st
;
841 st
= malloc(sizeof(*st
));
845 st
->max_devs
= orig
->max_devs
;
846 st
->minor_version
= orig
->minor_version
;
852 struct supertype
*guess_super(int fd
)
854 /* try each load_super to find the best match,
855 * and return the best superswitch
857 struct superswitch
*ss
;
858 struct supertype
*st
;
859 unsigned long besttime
= 0;
863 st
= malloc(sizeof(*st
));
864 memset(st
, 0, sizeof(*st
));
865 for (i
=0 ; superlist
[i
]; i
++) {
869 rv
= ss
->load_super(st
, fd
, NULL
);
872 st
->ss
->getinfo_super(st
, &info
);
873 if (bestsuper
== -1 ||
874 besttime
< info
.array
.ctime
) {
876 besttime
= info
.array
.ctime
;
881 if (bestsuper
!= -1) {
884 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
886 superlist
[bestsuper
]->free_super(st
);
894 /* Return size of device in bytes */
895 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
897 unsigned long long ldsize
;
900 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
901 ldsize
= (unsigned long long)st
.st_size
;
904 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
908 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
913 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
914 dname
, strerror(errno
));
922 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
925 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
926 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
927 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
931 int open_container(int fd
)
933 /* 'fd' is a block device. Find out if it is in use
934 * by a container, and return an open fd on that container.
945 if (fstat(fd
, &st
) != 0)
947 sprintf(path
, "/sys/dev/block/%d:%d/holders",
948 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
949 e
= path
+ strlen(path
);
954 while ((de
= readdir(dir
))) {
957 if (de
->d_name
[0] == '.')
959 sprintf(e
, "/%s/dev", de
->d_name
);
960 dfd
= open(path
, O_RDONLY
);
963 n
= read(dfd
, buf
, sizeof(buf
));
965 if (n
<= 0 || n
>= sizeof(buf
))
968 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
970 sprintf(buf
, "%d:%d", major
, minor
);
971 dfd
= dev_open(buf
, O_RDONLY
);
981 char *devnum2devname(int num
)
985 sprintf(name
, "md%d", num
);
987 sprintf(name
, "md_d%d", -1-num
);
991 int fd2devnum(int fd
)
994 if (fstat(fd
, &stb
) == 0 &&
995 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
996 if (major(stb
.st_rdev
) == MD_MAJOR
)
997 return minor(stb
.st_rdev
);
999 return -1- (minor(stb
.st_rdev
)>>6);
1004 int mdmon_running(int devnum
)
1010 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1011 fd
= open(path
, O_RDONLY
, 0);
1015 n
= read(fd
, pid
, 9);
1019 if (kill(atoi(pid
), 0) == 0)
1024 int signal_mdmon(int devnum
)
1030 sprintf(path
, "/var/run/mdadm/%s.pid", devnum2devname(devnum
));
1031 fd
= open(path
, O_RDONLY
, 0);
1035 n
= read(fd
, pid
, 9);
1039 if (kill(atoi(pid
), SIGUSR1
) == 0)
1047 /* tinyc doesn't optimize this check in ioctl.h out ... */
1048 unsigned int __invalid_size_argument_for_IOC
= 0;