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>
37 * following taken from linux/blkpg.h because they aren't
38 * anywhere else and it isn't safe to #include linux/ * stuff.
41 #define BLKPG _IO(0x12,105)
43 /* The argument structure */
44 struct blkpg_ioctl_arg
{
51 /* The subfunctions (for the op field) */
52 #define BLKPG_ADD_PARTITION 1
53 #define BLKPG_DEL_PARTITION 2
55 /* Sizes of name fields. Unused at present. */
56 #define BLKPG_DEVNAMELTH 64
57 #define BLKPG_VOLNAMELTH 64
59 /* The data structure for ADD_PARTITION and DEL_PARTITION */
60 struct blkpg_partition
{
61 long long start
; /* starting offset in bytes */
62 long long length
; /* length in bytes */
63 int pno
; /* partition number */
64 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
65 to be used in kernel messages */
66 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
70 * Parse a 128 bit uuid in 4 integers
71 * format is 32 hexx nibbles with options :.<space> separator
72 * If not exactly 32 hex digits are found, return 0
75 int parse_uuid(char *str
, int uuid
[4])
77 int hit
= 0; /* number of Hex digIT */
80 for (i
=0; i
<4; i
++) uuid
[i
]=0;
86 else if (c
>='a' && c
<= 'f')
88 else if (c
>='A' && c
<= 'F')
90 else if (strchr(":. -", c
))
107 * Get the md version number.
108 * We use the RAID_VERSION ioctl if it is supported
109 * If not, but we have a block device with major '9', we assume
112 * Return version number as 24 but number - assume version parts
116 int md_get_version(int fd
)
121 if (fstat(fd
, &stb
)<0)
123 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
126 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
127 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
130 if (major(stb
.st_rdev
) == MD_MAJOR
)
135 int get_linux_version()
144 a
= strtoul(cp
, &cp
, 10);
145 if (*cp
!= '.') return -1;
146 b
= strtoul(cp
+1, &cp
, 10);
147 if (*cp
!= '.') return -1;
148 c
= strtoul(cp
+1, NULL
, 10);
150 return (a
*1000000)+(b
*1000)+c
;
153 void remove_partitions(int fd
)
155 /* remove partitions from this block devices.
156 * This is used for components added to an array
158 #ifdef BLKPG_DEL_PARTITION
159 struct blkpg_ioctl_arg a
;
160 struct blkpg_partition p
;
162 a
.op
= BLKPG_DEL_PARTITION
;
164 a
.datalen
= sizeof(p
);
166 memset(a
.data
, 0, a
.datalen
);
167 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
168 ioctl(fd
, BLKPG
, &a
);
172 int enough(int level
, int raid_disks
, int layout
, int clean
,
173 char *avail
, int avail_disks
)
178 /* This is the tricky one - we need to check
179 * which actual disks are present.
181 copies
= (layout
&255)* ((layout
>>8) & 255);
184 /* there must be one of the 'copies' form 'first' */
190 first
= (first
+1) % raid_disks
;
195 } while (first
!= 0);
199 return avail_disks
>= 1;
202 return avail_disks
== raid_disks
;
204 return avail_disks
>= 1;
208 return avail_disks
>= raid_disks
-1;
210 return avail_disks
>= raid_disks
;
213 return avail_disks
>= raid_disks
-2;
215 return avail_disks
>= raid_disks
;
221 int same_uuid(int a
[4], int b
[4], int swapuuid
)
224 /* parse uuids are hostendian.
225 * uuid's from some superblocks are big-ending
226 * if there is a difference, we need to swap..
228 unsigned char *ac
= (unsigned char *)a
;
229 unsigned char *bc
= (unsigned char *)b
;
231 for (i
=0; i
<16; i
+= 4) {
232 if (ac
[i
+0] != bc
[i
+3] ||
233 ac
[i
+1] != bc
[i
+2] ||
234 ac
[i
+2] != bc
[i
+1] ||
248 void copy_uuid(void *a
, int b
[4], int swapuuid
)
251 /* parse uuids are hostendian.
252 * uuid's from some superblocks are big-ending
253 * if there is a difference, we need to swap..
255 unsigned char *ac
= (unsigned char *)a
;
256 unsigned char *bc
= (unsigned char *)b
;
258 for (i
=0; i
<16; i
+= 4) {
269 int check_ext2(int fd
, char *name
)
272 * Check for an ext2fs file system.
273 * Superblock is always 1K at 1K offset
275 * s_magic is le16 at 56 == 0xEF53
276 * report mtime - le32 at 44
278 * logblksize - le32 at 24
280 unsigned char sb
[1024];
283 if (lseek(fd
, 1024,0)!= 1024)
285 if (read(fd
, sb
, 1024)!= 1024)
287 if (sb
[56] != 0x53 || sb
[57] != 0xef)
290 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
291 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
292 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
293 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
295 fprintf(stderr
," size=%dK mtime=%s",
296 size
*(1<<bsize
), ctime(&mtime
));
300 int check_reiser(int fd
, char *name
)
303 * superblock is at 64K
305 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
308 unsigned char sb
[1024];
310 if (lseek(fd
, 64*1024, 0) != 64*1024)
312 if (read(fd
, sb
, 1024) != 1024)
314 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
315 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
317 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
318 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
319 fprintf(stderr
, " size = %luK\n", size
*4);
324 int check_raid(int fd
, char *name
)
329 struct supertype
*st
= guess_super(fd
);
332 st
->ss
->load_super(st
, fd
, name
);
333 /* Looks like a raid array .. */
334 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
336 st
->ss
->getinfo_super(st
, &info
);
337 st
->ss
->free_super(st
);
338 crtime
= info
.array
.ctime
;
339 level
= map_num(pers
, info
.array
.level
);
340 if (!level
) level
= "-unknown-";
341 fprintf(stderr
, " level=%s devices=%d ctime=%s",
342 level
, info
.array
.raid_disks
, ctime(&crtime
));
350 for (i
=0; i
<5; i
++) {
352 fprintf(stderr
, "%s%s", mesg
, add
);
354 if (fgets(buf
, 100, stdin
)==NULL
)
356 if (buf
[0]=='y' || buf
[0]=='Y')
358 if (buf
[0]=='n' || buf
[0]=='N')
362 fprintf(stderr
, Name
": assuming 'no'\n");
365 #endif /* MDASSEMBLE */
367 char *map_num(mapping_t
*map
, int num
)
377 int map_name(mapping_t
*map
, char *name
)
380 if (strcmp(map
->name
, name
)==0)
388 int is_standard(char *dev
, int *nump
)
390 /* tests if dev is a "standard" md dev name.
391 * i.e if the last component is "/dNN" or "/mdNN",
392 * where NN is a string of digits
393 * Returns 1 if a partitionable standard,
394 * -1 if non-partitonable,
395 * 0 if not a standard name.
397 char *d
= strrchr(dev
, '/');
402 if (strncmp(d
, "/d",2)==0)
403 d
+= 2, type
=1; /* /dev/md/dN{pM} */
404 else if (strncmp(d
, "/md_d", 5)==0)
405 d
+= 5, type
=1; /* /dev/md_dNpM */
406 else if (strncmp(d
, "/md", 3)==0)
407 d
+= 3, type
=-1; /* /dev/mdN */
408 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
409 d
+= 1, type
=-1; /* /dev/md/N */
419 if (nump
) *nump
= num
;
426 * convert a major/minor pair for a block device into a name in /dev, if possible.
427 * On the first call, walk /dev collecting name.
428 * Put them in a simple linked listfor now.
435 int devlist_ready
= 0;
437 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
440 if (S_ISLNK(stb
->st_mode
)) {
445 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
446 char *n
= strdup(name
);
447 struct devmap
*dm
= malloc(sizeof(*dm
));
448 if (strncmp(n
, "/dev/./", 7)==0)
451 dm
->major
= major(stb
->st_rdev
);
452 dm
->minor
= minor(stb
->st_rdev
);
463 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
465 return add_dev(name
, stb
, flag
, NULL
);
467 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
469 return ftw(path
, add_dev_1
, nopenfd
);
472 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
476 #endif /* HAVE_FTW */
477 #endif /* HAVE_NFTW */
480 * Find a block device with the right major/minor number.
481 * If we find multiple names, choose the shortest.
482 * If we find a non-standard name, it is probably there
483 * deliberately so prefer it over a standard name.
484 * This applies only to names for MD devices.
486 char *map_dev(int major
, int minor
, int create
)
489 char *std
= NULL
, *nonstd
=NULL
;
492 if (major
== 0 && minor
== 0)
496 if (!devlist_ready
) {
500 struct devmap
*d
= devlist
;
505 if (lstat(dev
, &stb
)==0 &&
506 S_ISLNK(stb
.st_mode
))
508 nftw(dev
, add_dev
, 10, FTW_PHYS
);
513 for (p
=devlist
; p
; p
=p
->next
)
514 if (p
->major
== major
&&
516 if (is_standard(p
->name
, NULL
)) {
518 strlen(p
->name
) < strlen(std
))
521 if (nonstd
== NULL
||
522 strlen(p
->name
) < strlen(nonstd
))
526 if (!std
&& !nonstd
&& !did_check
) {
530 if (create
&& !std
&& !nonstd
) {
532 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
536 return nonstd
? nonstd
: std
;
539 unsigned long calc_csum(void *super
, int bytes
)
541 unsigned long long newcsum
= 0;
544 unsigned int *superc
= (unsigned int*) super
;
546 for(i
=0; i
<bytes
/4; i
++)
548 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
550 /* The in-kernel checksum calculation is always 16bit on
551 * the alpha, though it is 32 bit on i386...
552 * I wonder what it is elsewhere... (it uses and API in
553 * a way that it shouldn't).
555 csum
= (csum
& 0xffff) + (csum
>> 16);
556 csum
= (csum
& 0xffff) + (csum
>> 16);
562 char *human_size(long long bytes
)
566 /* We convert bytes to either centi-M{ega,ibi}bytes or
567 * centi-G{igi,ibi}bytes, with appropriate rounding,
568 * and then print 1/100th of those as a decimal.
569 * We allow upto 2048Megabytes before converting to
570 * gigabytes, as that shows more precision and isn't
571 * too large a number.
572 * Terrabytes are not yet handled.
575 if (bytes
< 5000*1024)
577 else if (bytes
< 2*1024LL*1024LL*1024LL) {
578 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
579 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
580 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
581 cMiB
/100 , cMiB
% 100,
584 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
585 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
586 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
587 cGiB
/100 , cGiB
% 100,
593 char *human_size_brief(long long bytes
)
597 if (bytes
< 5000*1024)
598 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
599 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
601 else if (bytes
< 2*1024LL*1024LL*1024LL)
602 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
604 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
607 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
609 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
615 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
616 int chunksize
, unsigned long long devsize
)
620 case 0: data_disks
= raid_disks
; break;
621 case 1: data_disks
= 1; break;
623 case 5: data_disks
= raid_disks
- 1; break;
624 case 6: data_disks
= raid_disks
- 2; break;
625 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
628 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
629 return data_disks
* devsize
;
632 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
633 int get_mdp_major(void)
635 static int mdp_major
= -1;
639 int have_devices
= 0;
644 fl
= fopen("/proc/devices", "r");
647 while ((w
= conf_word(fl
, 1))) {
648 if (have_block
&& strcmp(w
, "devices:")==0)
650 have_block
= (strcmp(w
, "Block")==0);
653 if (have_devices
&& strcmp(w
, "mdp")==0)
654 mdp_major
= last_num
;
663 char *get_md_name(int dev
)
665 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
666 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
667 static char devname
[50];
673 int mdp
= get_mdp_major();
674 if (mdp
< 0) return NULL
;
675 rdev
= makedev(mdp
, (-1-dev
)<<6);
676 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
677 if (stat(devname
, &stb
) == 0
678 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
679 && (stb
.st_rdev
== rdev
))
682 rdev
= makedev(MD_MAJOR
, dev
);
683 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
684 if (stat(devname
, &stb
) == 0
685 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
686 && (stb
.st_rdev
== rdev
))
689 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
690 if (stat(devname
, &stb
) == 0
691 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
692 && (stb
.st_rdev
== rdev
))
695 dn
= map_dev(major(rdev
), minor(rdev
), 0);
698 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
699 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
703 if (stat(devname
, &stb
) == 0
704 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
705 && (stb
.st_rdev
== rdev
))
711 void put_md_name(char *name
)
713 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
717 static int dev2major(int d
)
722 return get_mdp_major();
725 static int dev2minor(int d
)
729 return (-1-d
) << MdpMinorShift
;
732 int find_free_devnum(int use_partitions
)
735 for (devnum
= 127; devnum
!= 128;
736 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
740 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
741 if (mddev_busy(_devnum
))
743 /* make sure it is new to /dev too, at least as a
745 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
746 if (dn
&& ! is_standard(dn
, NULL
))
752 return use_partitions
? (-1-devnum
) : devnum
;
754 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
756 int dev_open(char *dev
, int flags
)
758 /* like 'open', but if 'dev' matches %d:%d, create a temp
759 * block device and open that
769 major
= strtoul(dev
, &e
, 0);
770 if (e
> dev
&& *e
== ':' && e
[1] &&
771 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
773 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d", major
, minor
);
774 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
775 fd
= open(devname
, flags
);
779 fd
= open(dev
, flags
);
783 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, &super_imsm
, NULL
};
785 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
787 struct supertype supertype_container_member
;
789 struct supertype
*super_by_fd(int fd
)
791 mdu_array_info_t array
;
794 struct supertype
*st
= NULL
;
800 sra
= sysfs_read(fd
, 0, GET_VERSION
);
803 vers
= sra
->array
.major_version
;
804 minor
= sra
->array
.minor_version
;
805 verstr
= sra
->text_version
;
807 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
808 array
.major_version
= array
.minor_version
= 0;
809 vers
= array
.major_version
;
810 minor
= array
.minor_version
;
815 sprintf(version
, "%d.%d", vers
, minor
);
818 if (minor
== -2 && verstr
[0] == '/')
819 st
= &supertype_container_member
;
821 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
822 st
= superlist
[i
]->match_metadata_desc(verstr
);
830 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
833 struct supertype
*dup_super(struct supertype
*st
)
835 struct supertype
*stnew
= NULL
;
843 if (st
->ss
->text_version
)
844 strcpy(version
, st
->ss
->text_version
);
845 else if (st
->minor_version
== -1)
846 sprintf(version
, "%d", st
->ss
->major
);
848 sprintf(version
, "%d.%d", st
->ss
->major
, st
->minor_version
);
851 for (i
= 0; stnew
== NULL
&& superlist
[i
] ; i
++)
852 stnew
= superlist
[i
]->match_metadata_desc(verstr
);
859 struct supertype
*guess_super(int fd
)
861 /* try each load_super to find the best match,
862 * and return the best superswitch
864 struct superswitch
*ss
;
865 struct supertype
*st
;
866 unsigned long besttime
= 0;
870 st
= malloc(sizeof(*st
));
871 memset(st
, 0, sizeof(*st
));
872 for (i
=0 ; superlist
[i
]; i
++) {
876 rv
= ss
->load_super(st
, fd
, NULL
);
879 st
->ss
->getinfo_super(st
, &info
);
880 if (bestsuper
== -1 ||
881 besttime
< info
.array
.ctime
) {
883 besttime
= info
.array
.ctime
;
888 if (bestsuper
!= -1) {
891 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
893 superlist
[bestsuper
]->free_super(st
);
901 /* Return size of device in bytes */
902 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
904 unsigned long long ldsize
;
907 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
908 ldsize
= (unsigned long long)st
.st_size
;
911 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
915 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
920 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
921 dname
, strerror(errno
));
929 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
932 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
933 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
934 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
938 int open_container(int fd
)
940 /* 'fd' is a block device. Find out if it is in use
941 * by a container, and return an open fd on that container.
952 if (fstat(fd
, &st
) != 0)
954 sprintf(path
, "/sys/dev/block/%d:%d/holders",
955 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
956 e
= path
+ strlen(path
);
961 while ((de
= readdir(dir
))) {
964 if (de
->d_name
[0] == '.')
966 sprintf(e
, "/%s/dev", de
->d_name
);
967 dfd
= open(path
, O_RDONLY
);
970 n
= read(dfd
, buf
, sizeof(buf
));
972 if (n
<= 0 || n
>= sizeof(buf
))
975 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
977 sprintf(buf
, "%d:%d", major
, minor
);
978 dfd
= dev_open(buf
, O_RDONLY
);
987 char *devnum2devname(int num
)
991 sprintf(name
, "md%d", num
);
993 sprintf(name
, "md_d%d", -1-num
);
997 int fd2devnum(int fd
)
1000 if (fstat(fd
, &stb
) == 0 &&
1001 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
1002 if (major(stb
.st_rdev
) == MD_MAJOR
)
1003 return minor(stb
.st_rdev
);
1005 return -1- (minor(stb
.st_rdev
)>>6);
1011 /* tinyc doesn't optimize this check in ioctl.h out ... */
1012 unsigned int __invalid_size_argument_for_IOC
= 0;