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
))
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
)
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
)
331 struct supertype
*st
= guess_super(fd
);
334 st
->ss
->load_super(st
, fd
, &super
, name
);
335 /* Looks like a raid array .. */
336 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
338 st
->ss
->getinfo_super(&info
, super
);
340 crtime
= info
.array
.ctime
;
341 level
= map_num(pers
, info
.array
.level
);
342 if (!level
) level
= "-unknown-";
343 fprintf(stderr
, " level=%s devices=%d ctime=%s",
344 level
, info
.array
.raid_disks
, ctime(&crtime
));
352 for (i
=0; i
<5; i
++) {
354 fprintf(stderr
, "%s%s", mesg
, add
);
356 if (fgets(buf
, 100, stdin
)==NULL
)
358 if (buf
[0]=='y' || buf
[0]=='Y')
360 if (buf
[0]=='n' || buf
[0]=='N')
364 fprintf(stderr
, Name
": assuming 'no'\n");
367 #endif /* MDASSEMBLE */
369 char *map_num(mapping_t
*map
, int num
)
379 int map_name(mapping_t
*map
, char *name
)
382 if (strcmp(map
->name
, name
)==0)
390 int is_standard(char *dev
, int *nump
)
392 /* tests if dev is a "standard" md dev name.
393 * i.e if the last component is "/dNN" or "/mdNN",
394 * where NN is a string of digits
396 char *d
= strrchr(dev
, '/');
401 if (strncmp(d
, "/d",2)==0)
402 d
+= 2, type
=1; /* /dev/md/dN{pM} */
403 else if (strncmp(d
, "/md_d", 5)==0)
404 d
+= 5, type
=1; /* /dev/md_dNpM */
405 else if (strncmp(d
, "/md", 3)==0)
406 d
+= 3, type
=-1; /* /dev/mdN */
407 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
408 d
+= 1, type
=-1; /* /dev/md/N */
418 if (nump
) *nump
= num
;
425 * convert a major/minor pair for a block device into a name in /dev, if possible.
426 * On the first call, walk /dev collecting name.
427 * Put them in a simple linked listfor now.
434 int devlist_ready
= 0;
436 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
439 if (S_ISLNK(stb
->st_mode
)) {
444 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
445 char *n
= strdup(name
);
446 struct devmap
*dm
= malloc(sizeof(*dm
));
447 if (strncmp(n
, "/dev/./", 7)==0)
450 dm
->major
= major(stb
->st_rdev
);
451 dm
->minor
= minor(stb
->st_rdev
);
462 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
464 return add_dev(name
, stb
, flag
, NULL
);
466 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
468 return ftw(path
, add_dev_1
, nopenfd
);
471 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
475 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
479 #endif /* HAVE_FTW */
480 #endif /* HAVE_NFTW */
483 * Find a block device with the right major/minor number.
484 * If we find multiple names, choose the shortest.
485 * If we find a non-standard name, it is probably there
486 * deliberately so prefer it over a standard name.
487 * This applies only to names for MD devices.
489 char *map_dev(int major
, int minor
, int create
)
492 char *std
= NULL
, *nonstd
=NULL
;
495 if (major
== 0 && minor
== 0)
499 if (!devlist_ready
) {
503 struct devmap
*d
= devlist
;
508 if (lstat(dev
, &stb
)==0 &&
509 S_ISLNK(stb
.st_mode
))
511 nftw(dev
, add_dev
, 10, FTW_PHYS
);
516 for (p
=devlist
; p
; p
=p
->next
)
517 if (p
->major
== major
&&
519 if (is_standard(p
->name
, NULL
)) {
521 strlen(p
->name
) < strlen(std
))
524 if (nonstd
== NULL
||
525 strlen(p
->name
) < strlen(nonstd
))
529 if (!std
&& !nonstd
&& !did_check
) {
533 if (create
&& !std
&& !nonstd
) {
535 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
539 return nonstd
? nonstd
: std
;
542 unsigned long calc_csum(void *super
, int bytes
)
544 unsigned long long newcsum
= 0;
547 unsigned int *superc
= (unsigned int*) super
;
549 for(i
=0; i
<bytes
/4; i
++)
551 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
553 /* The in-kernel checksum calculation is always 16bit on
554 * the alpha, though it is 32 bit on i386...
555 * I wonder what it is elsewhere... (it uses and API in
556 * a way that it shouldn't).
558 csum
= (csum
& 0xffff) + (csum
>> 16);
559 csum
= (csum
& 0xffff) + (csum
>> 16);
565 char *human_size(long long bytes
)
569 /* We convert bytes to either centi-M{ega,ibi}bytes or
570 * centi-G{igi,ibi}bytes, with appropriate rounding,
571 * and then print 1/100th of those as a decimal.
572 * We allow upto 2048Megabytes before converting to
573 * gigabytes, as that shows more precision and isn't
574 * too large a number.
575 * Terrabytes are not yet handled.
578 if (bytes
< 5000*1024)
580 else if (bytes
< 2*1024LL*1024LL*1024LL) {
581 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
582 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
583 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
584 cMiB
/100 , cMiB
% 100,
587 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
588 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
589 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
590 cGiB
/100 , cGiB
% 100,
596 char *human_size_brief(long long bytes
)
601 if (bytes
< 5000*1024)
602 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
603 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
605 else if (bytes
< 2*1024LL*1024LL*1024LL)
606 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
608 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
611 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
613 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
619 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
620 int get_mdp_major(void)
622 static int mdp_major
= -1;
626 int have_devices
= 0;
631 fl
= fopen("/proc/devices", "r");
634 while ((w
= conf_word(fl
, 1))) {
635 if (have_block
&& strcmp(w
, "devices:")==0)
637 have_block
= (strcmp(w
, "Block")==0);
640 if (have_devices
&& strcmp(w
, "mdp")==0)
641 mdp_major
= last_num
;
650 char *get_md_name(int dev
)
652 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
653 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
654 static char devname
[50];
660 int mdp
= get_mdp_major();
661 if (mdp
< 0) return NULL
;
662 rdev
= makedev(mdp
, (-1-dev
)<<6);
663 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
664 if (stat(devname
, &stb
) == 0
665 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
666 && (stb
.st_rdev
== rdev
))
669 rdev
= makedev(MD_MAJOR
, dev
);
670 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
671 if (stat(devname
, &stb
) == 0
672 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
673 && (stb
.st_rdev
== rdev
))
676 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
677 if (stat(devname
, &stb
) == 0
678 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
679 && (stb
.st_rdev
== rdev
))
682 dn
= map_dev(major(rdev
), minor(rdev
), 0);
685 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
686 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
690 if (stat(devname
, &stb
) == 0
691 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
692 && (stb
.st_rdev
== rdev
))
698 void put_md_name(char *name
)
700 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
703 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
705 int dev_open(char *dev
, int flags
)
707 /* like 'open', but if 'dev' matches %d:%d, create a temp
708 * block device and open that
718 major
= strtoul(dev
, &e
, 0);
719 if (e
> dev
&& *e
== ':' && e
[1] &&
720 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
722 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d", major
, minor
);
723 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
724 fd
= open(devname
, flags
);
728 fd
= open(dev
, flags
);
732 struct superswitch
*superlist
[] = { &super0
, &super1
, NULL
};
734 struct supertype
*super_by_version(int vers
, int minor
)
736 struct supertype
*st
= malloc(sizeof(*st
));
740 st
->max_devs
= MD_SB_DISKS
;
747 st
->minor_version
= minor
;
751 struct supertype
*guess_super(int fd
)
753 /* try each load_super to find the best match,
754 * and return the best superswitch
756 struct superswitch
*ss
;
757 struct supertype
*st
;
758 unsigned long besttime
= 0;
764 st
= malloc(sizeof(*st
));
765 memset(st
, 0, sizeof(*st
));
766 for (i
=0 ; superlist
[i
]; i
++) {
770 rv
= ss
->load_super(st
, fd
, &sbp
, NULL
);
773 ss
->getinfo_super(&info
, sbp
);
774 if (bestsuper
== -1 ||
775 besttime
< info
.array
.ctime
) {
777 besttime
= info
.array
.ctime
;
782 if (bestsuper
!= -1) {
785 rv
= superlist
[bestsuper
]->load_super(st
, fd
, &sbp
, NULL
);
795 /* Return size of device in bytes */
796 int get_dev_size(int fd
, char *dname
, unsigned long long *sizep
)
798 unsigned long long ldsize
;
800 if (ioctl(fd
, BLKGETSIZE64
, &ldsize
) != 0)
804 if (ioctl(fd
, BLKGETSIZE
, &dsize
) == 0) {
809 fprintf(stderr
, Name
": Cannot get size of %s: %s\b",
810 dname
, strerror(errno
));
818 void get_one_disk(int mdfd
, mdu_array_info_t
*ainf
, mdu_disk_info_t
*disk
)
821 ioctl(mdfd
, GET_ARRAY_INFO
, ainf
);
822 for (d
= 0 ; d
< ainf
->raid_disks
+ ainf
->nr_disks
; d
++)
823 if (ioctl(mdfd
, GET_DISK_INFO
, disk
) == 0)
827 /* tinyc doesn't optimize this check in ioctl.h out ... */
828 unsigned int __invalid_size_argument_for_IOC
= 0;