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>
36 * following taken from linux/blkpg.h because they aren't
37 * anywhere else and it isn't safe to #include linux/ * stuff.
40 #define BLKPG _IO(0x12,105)
42 /* The argument structure */
43 struct blkpg_ioctl_arg
{
50 /* The subfunctions (for the op field) */
51 #define BLKPG_ADD_PARTITION 1
52 #define BLKPG_DEL_PARTITION 2
54 /* Sizes of name fields. Unused at present. */
55 #define BLKPG_DEVNAMELTH 64
56 #define BLKPG_VOLNAMELTH 64
58 /* The data structure for ADD_PARTITION and DEL_PARTITION */
59 struct blkpg_partition
{
60 long long start
; /* starting offset in bytes */
61 long long length
; /* length in bytes */
62 int pno
; /* partition number */
63 char devname
[BLKPG_DEVNAMELTH
]; /* partition name, like sda5 or c0d1p2,
64 to be used in kernel messages */
65 char volname
[BLKPG_VOLNAMELTH
]; /* volume label */
69 * Parse a 128 bit uuid in 4 integers
70 * format is 32 hexx nibbles with options :.<space> separator
71 * If not exactly 32 hex digits are found, return 0
74 int parse_uuid(char *str
, int uuid
[4])
76 int hit
= 0; /* number of Hex digIT */
79 for (i
=0; i
<4; i
++) uuid
[i
]=0;
85 else if (c
>='a' && c
<= 'f')
87 else if (c
>='A' && c
<= 'F')
89 else if (strchr(":. -", c
))
106 * Get the md version number.
107 * We use the RAID_VERSION ioctl if it is supported
108 * If not, but we have a block device with major '9', we assume
111 * Return version number as 24 but number - assume version parts
115 int md_get_version(int fd
)
120 if (fstat(fd
, &stb
)<0)
122 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
125 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
126 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
129 if (major(stb
.st_rdev
) == MD_MAJOR
)
134 int get_linux_version()
143 a
= strtoul(cp
, &cp
, 10);
144 if (*cp
!= '.') return -1;
145 b
= strtoul(cp
+1, &cp
, 10);
146 if (*cp
!= '.') return -1;
147 c
= strtoul(cp
+1, NULL
, 10);
149 return (a
*1000000)+(b
*1000)+c
;
152 void remove_partitions(int fd
)
154 /* remove partitions from this block devices.
155 * This is used for components added to an array
157 #ifdef BLKPG_DEL_PARTITION
158 struct blkpg_ioctl_arg a
;
159 struct blkpg_partition p
;
161 a
.op
= BLKPG_DEL_PARTITION
;
163 a
.datalen
= sizeof(p
);
165 memset(a
.data
, 0, a
.datalen
);
166 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
167 ioctl(fd
, BLKPG
, &a
);
171 int enough(int level
, int raid_disks
, int layout
, int clean
,
172 char *avail
, int avail_disks
)
177 /* This is the tricky one - we need to check
178 * which actual disks are present.
180 copies
= (layout
&255)* ((layout
>>8) & 255);
183 /* there must be one of the 'copies' form 'first' */
189 first
= (first
+1) % raid_disks
;
194 } while (first
!= 0);
198 return avail_disks
>= 1;
201 return avail_disks
== raid_disks
;
203 return avail_disks
>= 1;
207 return avail_disks
>= raid_disks
-1;
209 return avail_disks
>= raid_disks
;
212 return avail_disks
>= raid_disks
-2;
214 return avail_disks
>= raid_disks
;
220 int same_uuid(int a
[4], int b
[4], int swapuuid
)
223 /* parse uuids are hostendian.
224 * uuid's from some superblocks are big-ending
225 * if there is a difference, we need to swap..
227 unsigned char *ac
= (unsigned char *)a
;
228 unsigned char *bc
= (unsigned char *)b
;
230 for (i
=0; i
<16; i
+= 4) {
231 if (ac
[i
+0] != bc
[i
+3] ||
232 ac
[i
+1] != bc
[i
+2] ||
233 ac
[i
+2] != bc
[i
+1] ||
247 void copy_uuid(void *a
, int b
[4], int swapuuid
)
250 /* parse uuids are hostendian.
251 * uuid's from some superblocks are big-ending
252 * if there is a difference, we need to swap..
254 unsigned char *ac
= (unsigned char *)a
;
255 unsigned char *bc
= (unsigned char *)b
;
257 for (i
=0; i
<16; i
+= 4) {
268 int check_ext2(int fd
, char *name
)
271 * Check for an ext2fs file system.
272 * Superblock is always 1K at 1K offset
274 * s_magic is le16 at 56 == 0xEF53
275 * report mtime - le32 at 44
277 * logblksize - le32 at 24
279 unsigned char sb
[1024];
282 if (lseek(fd
, 1024,0)!= 1024)
284 if (read(fd
, sb
, 1024)!= 1024)
286 if (sb
[56] != 0x53 || sb
[57] != 0xef)
289 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
290 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
291 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
292 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
294 fprintf(stderr
," size=%dK mtime=%s",
295 size
*(1<<bsize
), ctime(&mtime
));
299 int check_reiser(int fd
, char *name
)
302 * superblock is at 64K
304 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
307 unsigned char sb
[1024];
309 if (lseek(fd
, 64*1024, 0) != 64*1024)
311 if (read(fd
, sb
, 1024) != 1024)
313 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
314 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
316 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
317 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
318 fprintf(stderr
, " size = %luK\n", size
*4);
323 int check_raid(int fd
, char *name
)
328 struct supertype
*st
= guess_super(fd
);
331 st
->ss
->load_super(st
, fd
, name
);
332 /* Looks like a raid array .. */
333 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
335 st
->ss
->getinfo_super(st
, &info
);
336 st
->ss
->free_super(st
);
337 crtime
= info
.array
.ctime
;
338 level
= map_num(pers
, info
.array
.level
);
339 if (!level
) level
= "-unknown-";
340 fprintf(stderr
, " level=%s devices=%d ctime=%s",
341 level
, info
.array
.raid_disks
, ctime(&crtime
));
349 for (i
=0; i
<5; i
++) {
351 fprintf(stderr
, "%s%s", mesg
, add
);
353 if (fgets(buf
, 100, stdin
)==NULL
)
355 if (buf
[0]=='y' || buf
[0]=='Y')
357 if (buf
[0]=='n' || buf
[0]=='N')
361 fprintf(stderr
, Name
": assuming 'no'\n");
364 #endif /* MDASSEMBLE */
366 char *map_num(mapping_t
*map
, int num
)
376 int map_name(mapping_t
*map
, char *name
)
379 if (strcmp(map
->name
, name
)==0)
387 int is_standard(char *dev
, int *nump
)
389 /* tests if dev is a "standard" md dev name.
390 * i.e if the last component is "/dNN" or "/mdNN",
391 * where NN is a string of digits
393 char *d
= strrchr(dev
, '/');
398 if (strncmp(d
, "/d",2)==0)
399 d
+= 2, type
=1; /* /dev/md/dN{pM} */
400 else if (strncmp(d
, "/md_d", 5)==0)
401 d
+= 5, type
=1; /* /dev/md_dN{pM} */
402 else if (strncmp(d
, "/md", 3)==0)
403 d
+= 3, type
=-1; /* /dev/mdN */
404 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
405 d
+= 1, type
=-1; /* /dev/md/N */
415 if (nump
) *nump
= num
;
422 * convert a major/minor pair for a block device into a name in /dev, if possible.
423 * On the first call, walk /dev collecting name.
424 * Put them in a simple linked listfor now.
431 int devlist_ready
= 0;
433 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
437 if (S_ISLNK(stb
->st_mode
)) {
438 if (stat(name
, &st
) != 0)
443 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
444 char *n
= strdup(name
);
445 struct devmap
*dm
= malloc(sizeof(*dm
));
446 if (strncmp(n
, "/dev/./", 7)==0)
449 dm
->major
= major(stb
->st_rdev
);
450 dm
->minor
= minor(stb
->st_rdev
);
461 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
463 return add_dev(name
, stb
, flag
, NULL
);
465 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
467 return ftw(path
, add_dev_1
, nopenfd
);
470 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
474 #endif /* HAVE_FTW */
475 #endif /* HAVE_NFTW */
478 * Find a block device with the right major/minor number.
479 * If we find multiple names, choose the shortest.
480 * If we find a non-standard name, it is probably there
481 * deliberately so prefer it over a standard name.
482 * This applies only to names for MD devices.
484 char *map_dev(int major
, int minor
, int create
)
487 char *std
= NULL
, *nonstd
=NULL
;
490 if (major
== 0 && minor
== 0)
494 if (!devlist_ready
) {
498 struct devmap
*d
= devlist
;
503 if (lstat(dev
, &stb
)==0 &&
504 S_ISLNK(stb
.st_mode
))
506 nftw(dev
, add_dev
, 10, FTW_PHYS
);
511 for (p
=devlist
; p
; p
=p
->next
)
512 if (p
->major
== major
&&
514 if (is_standard(p
->name
, NULL
)) {
516 strlen(p
->name
) < strlen(std
))
519 if (nonstd
== NULL
||
520 strlen(p
->name
) < strlen(nonstd
))
524 if (!std
&& !nonstd
&& !did_check
) {
528 if (create
&& !std
&& !nonstd
) {
530 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
534 return nonstd
? nonstd
: std
;
537 unsigned long calc_csum(void *super
, int bytes
)
539 unsigned long long newcsum
= 0;
542 unsigned int *superc
= (unsigned int*) super
;
544 for(i
=0; i
<bytes
/4; i
++)
546 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
548 /* The in-kernel checksum calculation is always 16bit on
549 * the alpha, though it is 32 bit on i386...
550 * I wonder what it is elsewhere... (it uses and API in
551 * a way that it shouldn't).
553 csum
= (csum
& 0xffff) + (csum
>> 16);
554 csum
= (csum
& 0xffff) + (csum
>> 16);
560 char *human_size(long long bytes
)
564 /* We convert bytes to either centi-M{ega,ibi}bytes or
565 * centi-G{igi,ibi}bytes, with appropriate rounding,
566 * and then print 1/100th of those as a decimal.
567 * We allow upto 2048Megabytes before converting to
568 * gigabytes, as that shows more precision and isn't
569 * too large a number.
570 * Terrabytes are not yet handled.
573 if (bytes
< 5000*1024)
575 else if (bytes
< 2*1024LL*1024LL*1024LL) {
576 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
577 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
578 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
579 cMiB
/100 , cMiB
% 100,
582 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
583 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
584 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
585 cGiB
/100 , cGiB
% 100,
591 char *human_size_brief(long long bytes
)
595 if (bytes
< 5000*1024)
596 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
597 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
599 else if (bytes
< 2*1024LL*1024LL*1024LL)
600 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
602 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
605 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
607 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
612 void print_r10_layout(int layout
)
614 int near
= layout
& 255;
615 int far
= (layout
>> 8) & 255;
616 int offset
= (layout
&0x10000);
620 printf("%s near=%d", sep
, near
);
624 printf("%s %s=%d", sep
, offset
?"offset":"far", far
);
626 printf("NO REDUNDANCY");
630 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
631 int get_mdp_major(void)
633 static int mdp_major
= -1;
637 int have_devices
= 0;
642 fl
= fopen("/proc/devices", "r");
645 while ((w
= conf_word(fl
, 1))) {
646 if (have_block
&& strcmp(w
, "devices:")==0)
648 have_block
= (strcmp(w
, "Block")==0);
651 if (have_devices
&& strcmp(w
, "mdp")==0)
652 mdp_major
= last_num
;
661 char *get_md_name(int dev
)
663 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
664 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
665 static char devname
[50];
671 int mdp
= get_mdp_major();
672 if (mdp
< 0) return NULL
;
673 rdev
= makedev(mdp
, (-1-dev
)<<6);
674 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
675 if (stat(devname
, &stb
) == 0
676 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
677 && (stb
.st_rdev
== rdev
))
680 rdev
= makedev(MD_MAJOR
, dev
);
681 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
682 if (stat(devname
, &stb
) == 0
683 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
684 && (stb
.st_rdev
== rdev
))
687 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
688 if (stat(devname
, &stb
) == 0
689 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
690 && (stb
.st_rdev
== rdev
))
693 dn
= map_dev(major(rdev
), minor(rdev
), 0);
696 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
697 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
701 if (stat(devname
, &stb
) == 0
702 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
703 && (stb
.st_rdev
== rdev
))
709 void put_md_name(char *name
)
711 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
715 static int dev2major(int d
)
720 return get_mdp_major();
723 static int dev2minor(int d
)
727 return (-1-d
) << MdpMinorShift
;
730 int find_free_devnum(int use_partitions
)
733 for (devnum
= 127; devnum
!= 128;
734 devnum
= devnum
? devnum
-1 : (1<<22)-1) {
738 _devnum
= use_partitions
? (-1-devnum
) : devnum
;
739 if (mddev_busy(_devnum
))
741 /* make sure it is new to /dev too, at least as a
743 dn
= map_dev(dev2major(_devnum
), dev2minor(_devnum
), 0);
744 if (dn
&& ! is_standard(dn
, NULL
))
750 return use_partitions
? (-1-devnum
) : devnum
;
752 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
754 int dev_open(char *dev
, int flags
)
756 /* like 'open', but if 'dev' matches %d:%d, create a temp
757 * block device and open that
767 major
= strtoul(dev
, &e
, 0);
768 if (e
> dev
&& *e
== ':' && e
[1] &&
769 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
771 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d", major
, minor
);
772 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
773 fd
= open(devname
, flags
);
777 fd
= open(dev
, flags
);
781 struct superswitch
*superlist
[] = { &super0
, &super1
, NULL
};
783 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
784 struct supertype
*super_by_fd(int fd
)
786 mdu_array_info_t array
;
789 struct supertype
*st
= NULL
;
795 sra
= sysfs_read(fd
, 0, GET_VERSION
);
798 vers
= sra
->array
.major_version
;
799 minor
= sra
->array
.minor_version
;
800 verstr
= sra
->text_version
;
802 if (ioctl(fd
, GET_ARRAY_INFO
, &array
))
803 array
.major_version
= array
.minor_version
= 0;
804 vers
= array
.major_version
;
805 minor
= array
.minor_version
;
810 sprintf(version
, "%d.%d", vers
, minor
);
813 for (i
= 0; st
== NULL
&& superlist
[i
] ; i
++)
814 st
= superlist
[i
]->match_metadata_desc(verstr
);
822 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
825 struct supertype
*dup_super(struct supertype
*st
)
827 struct supertype
*stnew
= NULL
;
835 if (st
->minor_version
== -1)
836 sprintf(version
, "%d", st
->ss
->major
);
838 sprintf(version
, "%d.%d", st
->ss
->major
, st
->minor_version
);
841 for (i
= 0; stnew
== NULL
&& superlist
[i
] ; i
++)
842 stnew
= superlist
[i
]->match_metadata_desc(verstr
);
849 struct supertype
*guess_super(int fd
)
851 /* try each load_super to find the best match,
852 * and return the best superswitch
854 struct superswitch
*ss
;
855 struct supertype
*st
;
856 unsigned long besttime
= 0;
860 st
= malloc(sizeof(*st
));
861 memset(st
, 0, sizeof(*st
));
862 for (i
=0 ; superlist
[i
]; i
++) {
866 rv
= ss
->load_super(st
, fd
, NULL
);
869 st
->ss
->getinfo_super(st
, &info
);
870 if (bestsuper
== -1 ||
871 besttime
< info
.array
.ctime
) {
873 besttime
= info
.array
.ctime
;
878 if (bestsuper
!= -1) {
881 rv
= superlist
[bestsuper
]->load_super(st
, fd
, NULL
);
883 superlist
[bestsuper
]->free_super(st
);
891 /* Return size of device in bytes */
892 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
894 unsigned long long ldsize
;
897 if (fstat(fd
, &st
) != -1 && S_ISREG(st
.st_mode
))
898 ldsize
= (unsigned long long)st
.st_size
;
901 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
905 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
910 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
911 dname
, strerror(errno
));
919 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
922 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
923 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
924 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
929 /* tinyc doesn't optimize this check in ioctl.h out ... */
930 unsigned int __invalid_size_argument_for_IOC
= 0;