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
394 char *d
= strrchr(dev
, '/');
399 if (strncmp(d
, "/d",2)==0)
400 d
+= 2, type
=1; /* /dev/md/dN{pM} */
401 else if (strncmp(d
, "/md_d", 5)==0)
402 d
+= 5, type
=1; /* /dev/md_dNpM */
403 else if (strncmp(d
, "/md", 3)==0)
404 d
+= 3, type
=-1; /* /dev/mdN */
405 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
406 d
+= 1, type
=-1; /* /dev/md/N */
416 if (nump
) *nump
= num
;
423 * convert a major/minor pair for a block device into a name in /dev, if possible.
424 * On the first call, walk /dev collecting name.
425 * Put them in a simple linked listfor now.
432 int devlist_ready
= 0;
434 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
437 if (S_ISLNK(stb
->st_mode
)) {
442 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
443 char *n
= strdup(name
);
444 struct devmap
*dm
= malloc(sizeof(*dm
));
445 if (strncmp(n
, "/dev/./", 7)==0)
448 dm
->major
= major(stb
->st_rdev
);
449 dm
->minor
= minor(stb
->st_rdev
);
460 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
462 return add_dev(name
, stb
, flag
, NULL
);
464 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
466 return ftw(path
, add_dev_1
, nopenfd
);
469 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
473 #endif /* HAVE_FTW */
474 #endif /* HAVE_NFTW */
477 * Find a block device with the right major/minor number.
478 * If we find multiple names, choose the shortest.
479 * If we find a non-standard name, it is probably there
480 * deliberately so prefer it over a standard name.
481 * This applies only to names for MD devices.
483 char *map_dev(int major
, int minor
, int create
)
486 char *std
= NULL
, *nonstd
=NULL
;
489 if (major
== 0 && minor
== 0)
493 if (!devlist_ready
) {
497 struct devmap
*d
= devlist
;
502 if (lstat(dev
, &stb
)==0 &&
503 S_ISLNK(stb
.st_mode
))
505 nftw(dev
, add_dev
, 10, FTW_PHYS
);
510 for (p
=devlist
; p
; p
=p
->next
)
511 if (p
->major
== major
&&
513 if (is_standard(p
->name
, NULL
)) {
515 strlen(p
->name
) < strlen(std
))
518 if (nonstd
== NULL
||
519 strlen(p
->name
) < strlen(nonstd
))
523 if (!std
&& !nonstd
&& !did_check
) {
527 if (create
&& !std
&& !nonstd
) {
529 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
533 return nonstd
? nonstd
: std
;
536 unsigned long calc_csum(void *super
, int bytes
)
538 unsigned long long newcsum
= 0;
541 unsigned int *superc
= (unsigned int*) super
;
543 for(i
=0; i
<bytes
/4; i
++)
545 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
547 /* The in-kernel checksum calculation is always 16bit on
548 * the alpha, though it is 32 bit on i386...
549 * I wonder what it is elsewhere... (it uses and API in
550 * a way that it shouldn't).
552 csum
= (csum
& 0xffff) + (csum
>> 16);
553 csum
= (csum
& 0xffff) + (csum
>> 16);
559 char *human_size(long long bytes
)
563 /* We convert bytes to either centi-M{ega,ibi}bytes or
564 * centi-G{igi,ibi}bytes, with appropriate rounding,
565 * and then print 1/100th of those as a decimal.
566 * We allow upto 2048Megabytes before converting to
567 * gigabytes, as that shows more precision and isn't
568 * too large a number.
569 * Terrabytes are not yet handled.
572 if (bytes
< 5000*1024)
574 else if (bytes
< 2*1024LL*1024LL*1024LL) {
575 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
576 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
577 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
578 cMiB
/100 , cMiB
% 100,
581 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
582 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
583 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
584 cGiB
/100 , cGiB
% 100,
590 char *human_size_brief(long long bytes
)
594 if (bytes
< 5000*1024)
595 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
596 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
598 else if (bytes
< 2*1024LL*1024LL*1024LL)
599 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
601 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
604 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
606 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
612 unsigned long long calc_array_size(int level
, int raid_disks
, int layout
,
613 int chunksize
, unsigned long long devsize
)
617 case 0: data_disks
= raid_disks
; break;
618 case 1: data_disks
= 1; break;
620 case 5: data_disks
= raid_disks
- 1; break;
621 case 6: data_disks
= raid_disks
- 2; break;
622 case 10: data_disks
= raid_disks
/ (layout
& 255) / ((layout
>>8)&255);
625 devsize
&= ~(unsigned long long)((chunksize
>>9)-1);
626 return data_disks
* devsize
;
629 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
630 int get_mdp_major(void)
632 static int mdp_major
= -1;
636 int have_devices
= 0;
641 fl
= fopen("/proc/devices", "r");
644 while ((w
= conf_word(fl
, 1))) {
645 if (have_block
&& strcmp(w
, "devices:")==0)
647 have_block
= (strcmp(w
, "Block")==0);
650 if (have_devices
&& strcmp(w
, "mdp")==0)
651 mdp_major
= last_num
;
660 char *get_md_name(int dev
)
662 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
663 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
664 static char devname
[50];
670 int mdp
= get_mdp_major();
671 if (mdp
< 0) return NULL
;
672 rdev
= makedev(mdp
, (-1-dev
)<<6);
673 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
674 if (stat(devname
, &stb
) == 0
675 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
676 && (stb
.st_rdev
== rdev
))
679 rdev
= makedev(MD_MAJOR
, dev
);
680 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
681 if (stat(devname
, &stb
) == 0
682 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
683 && (stb
.st_rdev
== rdev
))
686 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
687 if (stat(devname
, &stb
) == 0
688 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
689 && (stb
.st_rdev
== rdev
))
692 dn
= map_dev(major(rdev
), minor(rdev
), 0);
695 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
696 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
700 if (stat(devname
, &stb
) == 0
701 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
702 && (stb
.st_rdev
== rdev
))
708 void put_md_name(char *name
)
710 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
714 static int dev2major(int d
)
719 return get_mdp_major();
722 static int dev2minor(int d
)
726 return (-1-d
) << MdpMinorShift
;
729 int find_free_devnum(int use_partitions
)
732 for (devnum
= 127; devnum
!= 128;
733 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
737 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
738 if (mddev_busy(_devnum
))
740 /* make sure it is new to /dev too, at least as a
742 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
743 if (dn
&& ! is_standard(dn
, NULL
))
749 return use_partitions
? (-1-devnum
) : devnum
;
751 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
753 int dev_open(char *dev
, int flags
)
755 /* like 'open', but if 'dev' matches %d:%d, create a temp
756 * block device and open that
766 major
= strtoul(dev
, &e
, 0);
767 if (e
> dev
&& *e
== ':' && e
[1] &&
768 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
770 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d", major
, minor
);
771 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
772 fd
= open(devname
, flags
);
776 fd
= open(dev
, flags
);
780 struct superswitch
*superlist
[] = { &super0
, &super1
, &super_ddf
, NULL
};
782 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
783 struct supertype
*super_by_fd(int fd
)
785 mdu_array_info_t array
;
788 struct supertype
*st
= NULL
;
794 sra
= sysfs_read(fd
, 0, GET_VERSION
);
797 vers
= sra
->array
.major_version
;
798 minor
= sra
->array
.minor_version
;
799 verstr
= sra
->text_version
;
801 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
802 array
.major_version
= array
.minor_version
= 0;
803 vers
= array
.major_version
;
804 minor
= array
.minor_version
;
809 sprintf(version
, "%d.%d", vers
, minor
);
812 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
813 st
= superlist
[i
]->match_metadata_desc(verstr
);
821 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
824 struct supertype
*dup_super(struct supertype
*st
)
826 struct supertype
*stnew
= NULL
;
834 if (st
->ss
->text_version
)
835 strcpy(version
, st
->ss
->text_version
);
836 else if (st
->minor_version
== -1)
837 sprintf(version
, "%d", st
->ss
->major
);
839 sprintf(version
, "%d.%d", st
->ss
->major
, st
->minor_version
);
842 for (i
= 0; stnew
== NULL
&& superlist
[i
] ; i
++)
843 stnew
= superlist
[i
]->match_metadata_desc(verstr
);
850 struct supertype
*guess_super(int fd
)
852 /* try each load_super to find the best match,
853 * and return the best superswitch
855 struct superswitch
*ss
;
856 struct supertype
*st
;
857 unsigned long besttime
= 0;
861 st
= malloc(sizeof(*st
));
862 memset(st
, 0, sizeof(*st
));
863 for (i
=0 ; superlist
[i
]; i
++) {
867 rv
= ss
->load_super(st
, fd
, NULL
);
870 st
->ss
->getinfo_super(st
, &info
);
871 if (bestsuper
== -1 ||
872 besttime
< info
.array
.ctime
) {
874 besttime
= info
.array
.ctime
;
879 if (bestsuper
!= -1) {
882 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
884 superlist
[bestsuper
]->free_super(st
);
892 /* Return size of device in bytes */
893 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
895 unsigned long long ldsize
;
898 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
899 ldsize
= (unsigned long long)st
.st_size
;
902 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
906 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
911 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
912 dname
, strerror(errno
));
920 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
923 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
924 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
925 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
929 int open_container(int fd
)
931 /* 'fd' is a block device. Find out if it is in use
932 * by a container, and return an open fd on that container.
943 if (fstat(fd
, &st
) != 0)
945 sprintf(path
, "/sys/dev/block/%d:%d/holders",
946 (int)major(st
.st_rdev
), (int)minor(st
.st_rdev
));
947 e
= path
+ strlen(path
);
952 while ((de
= readdir(dir
))) {
955 if (de
->d_name
[0] == '.')
957 sprintf(e
, "/%s/dev", de
->d_name
);
958 dfd
= open(path
, O_RDONLY
);
961 n
= read(dfd
, buf
, sizeof(buf
));
963 if (n
<= 0 || n
>= sizeof(buf
))
966 if (sscanf(buf
, "%d:%d", &major
, &minor
) != 2)
968 sprintf(buf
, "%d:%d", major
, minor
);
969 dfd
= dev_open(buf
, O_RDONLY
);
979 /* tinyc doesn't optimize this check in ioctl.h out ... */
980 unsigned int __invalid_size_argument_for_IOC
= 0;