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>
34 #include <linux/blkpg.h>
37 * Parse a 128 bit uuid in 4 integers
38 * format is 32 hexx nibbles with options :.<space> separator
39 * If not exactly 32 hex digits are found, return 0
42 int parse_uuid(char *str
, int uuid
[4])
44 int hit
= 0; /* number of Hex digIT */
47 for (i
=0; i
<4; i
++) uuid
[i
]=0;
53 else if (c
>='a' && c
<= 'f')
55 else if (c
>='A' && c
<= 'F')
57 else if (strchr(":. -", c
))
75 * Get the md version number.
76 * We use the RAID_VERSION ioctl if it is supported
77 * If not, but we have a block device with major '9', we assume
80 * Return version number as 24 but number - assume version parts
84 int md_get_version(int fd
)
89 if (fstat(fd
, &stb
)<0)
91 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
94 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
95 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
98 if (major(stb
.st_rdev
) == MD_MAJOR
)
104 int get_linux_version()
113 a
= strtoul(cp
, &cp
, 10);
114 if (*cp
!= '.') return -1;
115 b
= strtoul(cp
+1, &cp
, 10);
116 if (*cp
!= '.') return -1;
117 c
= strtoul(cp
+1, NULL
, 10);
119 return (a
*1000000)+(b
*1000)+c
;
122 void remove_partitions(int fd
)
124 /* remove partitions from this block devices.
125 * This is used for components added to an array
127 #ifdef BLKPG_DEL_PARTITION
128 struct blkpg_ioctl_arg a
;
129 struct blkpg_partition p
;
131 a
.op
= BLKPG_DEL_PARTITION
;
133 a
.datalen
= sizeof(p
);
135 memset(a
.data
, 0, a
.datalen
);
136 for (p
.pno
=0; p
.pno
< 16; p
.pno
++)
137 ioctl(fd
, BLKPG
, &a
);
141 int enough(int level
, int raid_disks
, int layout
,
142 char *avail
, int avail_disks
)
147 /* This is the tricky one - we need to check
148 * which actual disks are present.
150 copies
= (layout
&255)* (layout
>>8);
153 /* there must be one of the 'copies' form 'first' */
159 first
= (first
+1) % raid_disks
;
164 } while (first
!= 0);
168 return avail_disks
>= 1;
171 return avail_disks
== raid_disks
;
173 return avail_disks
>= 1;
176 return avail_disks
>= raid_disks
-1;
178 return avail_disks
>= raid_disks
-2;
184 int same_uuid(int a
[4], int b
[4], int swapuuid
)
187 /* parse uuids are hostendian.
188 * uuid's from some superblocks are big-ending
189 * if there is a difference, we need to swap..
191 unsigned char *ac
= (unsigned char *)a
;
192 unsigned char *bc
= (unsigned char *)b
;
194 for (i
=0; i
<16; i
+= 4) {
195 if (ac
[i
+0] != bc
[i
+3] ||
196 ac
[i
+1] != bc
[i
+2] ||
197 ac
[i
+2] != bc
[i
+1] ||
212 int check_ext2(int fd
, char *name
)
215 * Check for an ext2fs file system.
216 * Superblock is always 1K at 1K offset
218 * s_magic is le16 at 56 == 0xEF53
219 * report mtime - le32 at 44
221 * logblksize - le32 at 24
223 unsigned char sb
[1024];
226 if (lseek(fd
, 1024,0)!= 1024)
228 if (read(fd
, sb
, 1024)!= 1024)
230 if (sb
[56] != 0x53 || sb
[57] != 0xef)
233 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
234 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
235 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
236 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
238 fprintf(stderr
," size=%dK mtime=%s",
239 size
*(1<<bsize
), ctime(&mtime
));
243 int check_reiser(int fd
, char *name
)
246 * superblock is at 64K
248 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
251 unsigned char sb
[1024];
253 if (lseek(fd
, 64*1024, 0) != 64*1024)
255 if (read(fd
, sb
, 1024) != 1024)
257 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
258 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
260 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
261 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
262 fprintf(stderr
, " size = %luK\n", size
*4);
267 int check_raid(int fd
, char *name
)
273 struct supertype
*st
= guess_super(fd
);
276 st
->ss
->load_super(st
, fd
, &super
, name
);
277 /* Looks like a raid array .. */
278 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
280 st
->ss
->getinfo_super(&info
, super
);
282 crtime
= info
.array
.ctime
;
283 level
= map_num(pers
, info
.array
.level
);
284 if (!level
) level
= "-unknown-";
285 fprintf(stderr
, " level=%s devices=%d ctime=%s",
286 level
, info
.array
.raid_disks
, ctime(&crtime
));
294 for (i
=0; i
<5; i
++) {
296 fprintf(stderr
, "%s%s", mesg
, add
);
298 if (fgets(buf
, 100, stdin
)==NULL
)
300 if (buf
[0]=='y' || buf
[0]=='Y')
302 if (buf
[0]=='n' || buf
[0]=='N')
306 fprintf(stderr
, Name
": assuming 'no'\n");
310 char *map_num(mapping_t
*map
, int num
)
320 int map_name(mapping_t
*map
, char *name
)
323 if (strcmp(map
->name
, name
)==0)
331 int is_standard(char *dev
, int *nump
)
333 /* tests if dev is a "standard" md dev name.
334 * i.e if the last component is "/dNN" or "/mdNN",
335 * where NN is a string of digits
337 char *d
= strrchr(dev
, '/');
342 if (strncmp(d
, "/d",2)==0)
343 d
+= 2, type
=1; /* /dev/md/dN{pM} */
344 else if (strncmp(d
, "/md_d", 5)==0)
345 d
+= 5, type
=1; /* /dev/md_dNpM */
346 else if (strncmp(d
, "/md", 3)==0)
347 d
+= 3, type
=-1; /* /dev/mdN */
348 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
349 d
+= 1, type
=-1; /* /dev/md/N */
359 if (nump
) *nump
= num
;
366 * convert a major/minor pair for a block device into a name in /dev, if possible.
367 * On the first call, walk /dev collecting name.
368 * Put them in a simple linked listfor now.
375 int devlist_ready
= 0;
377 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
380 if (S_ISLNK(stb
->st_mode
)) {
385 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
386 char *n
= strdup(name
);
387 struct devmap
*dm
= malloc(sizeof(*dm
));
388 if (strncmp(n
, "/dev/./", 7)==0)
391 dm
->major
= major(stb
->st_rdev
);
392 dm
->minor
= minor(stb
->st_rdev
);
403 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
405 return add_dev(name
, stb
, flag
, NULL
);
407 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
409 return ftw(path
, add_dev_1
, nopenfd
);
412 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
416 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
420 #endif /* HAVE_FTW */
421 #endif /* HAVE_NFTW */
424 * Find a block device with the right major/minor number.
425 * If we find multiple names, choose the shortest.
426 * If we find a non-standard name, it is probably there
427 * deliberately so prefer it over a standard name.
428 * This applies only to names for MD devices.
430 char *map_dev(int major
, int minor
, int create
)
433 char *std
= NULL
, *nonstd
=NULL
;
436 if (major
== 0 && minor
== 0)
440 if (!devlist_ready
) {
444 struct devmap
*d
= devlist
;
449 if (lstat(dev
, &stb
)==0 &&
450 S_ISLNK(stb
.st_mode
))
452 nftw(dev
, add_dev
, 10, FTW_PHYS
);
457 for (p
=devlist
; p
; p
=p
->next
)
458 if (p
->major
== major
&&
460 if (is_standard(p
->name
, NULL
)) {
462 strlen(p
->name
) < strlen(std
))
465 if (nonstd
== NULL
||
466 strlen(p
->name
) < strlen(nonstd
))
470 if (!std
&& !nonstd
&& !did_check
) {
474 if (create
&& !std
&& !nonstd
) {
476 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
480 return nonstd
? nonstd
: std
;
483 unsigned long calc_csum(void *super
, int bytes
)
485 unsigned long long newcsum
= 0;
488 unsigned int *superc
= (unsigned int*) super
;
490 for(i
=0; i
<bytes
/4; i
++)
492 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
494 /* The in-kernel checksum calculation is always 16bit on
495 * the alpha, though it is 32 bit on i386...
496 * I wonder what it is elsewhere... (it uses and API in
497 * a way that it shouldn't).
499 csum
= (csum
& 0xffff) + (csum
>> 16);
500 csum
= (csum
& 0xffff) + (csum
>> 16);
505 char *human_size(long long bytes
)
509 /* We convert bytes to either centi-M{ega,ibi}bytes or
510 * centi-G{igi,ibi}bytes, with appropriate rounding,
511 * and then print 1/100th of those as a decimal.
512 * We allow upto 2048Megabytes before converting to
513 * gigabytes, as that shows more precision and isn't
514 * too large a number.
515 * Terrabytes are not yet handled.
518 if (bytes
< 5000*1024)
520 else if (bytes
< 2*1024LL*1024LL*1024LL) {
521 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
522 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
523 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
524 cMiB
/100 , cMiB
% 100,
527 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
528 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
529 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
530 cGiB
/100 , cGiB
% 100,
536 char *human_size_brief(long long bytes
)
541 if (bytes
< 5000*1024)
542 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
543 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
545 else if (bytes
< 2*1024LL*1024LL*1024LL)
546 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
548 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
551 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
553 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
558 int get_mdp_major(void)
560 static int mdp_major
= -1;
564 int have_devices
= 0;
569 fl
= fopen("/proc/devices", "r");
572 while ((w
= conf_word(fl
, 1))) {
573 if (have_block
&& strcmp(w
, "devices:")==0)
575 have_block
= (strcmp(w
, "Block")==0);
578 if (have_devices
&& strcmp(w
, "mdp")==0)
579 mdp_major
= last_num
;
588 char *get_md_name(int dev
)
590 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
591 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
592 static char devname
[50];
598 int mdp
= get_mdp_major();
599 if (mdp
< 0) return NULL
;
600 rdev
= makedev(mdp
, (-1-dev
)<<6);
601 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
602 if (stat(devname
, &stb
) == 0
603 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
604 && (stb
.st_rdev
== rdev
))
607 rdev
= makedev(MD_MAJOR
, dev
);
608 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
609 if (stat(devname
, &stb
) == 0
610 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
611 && (stb
.st_rdev
== rdev
))
614 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
615 if (stat(devname
, &stb
) == 0
616 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
617 && (stb
.st_rdev
== rdev
))
620 dn
= map_dev(major(rdev
), minor(rdev
), 0);
623 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
624 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
628 if (stat(devname
, &stb
) == 0
629 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
630 && (stb
.st_rdev
== rdev
))
636 void put_md_name(char *name
)
638 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
642 int dev_open(char *dev
, int flags
)
644 /* like 'open', but if 'dev' matches %d:%d, create a temp
645 * block device and open that
655 major
= strtoul(dev
, &e
, 0);
656 if (e
> dev
&& *e
== ':' && e
[1] &&
657 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
659 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d", major
, minor
);
660 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
661 fd
= open(devname
, flags
);
665 fd
= open(dev
, flags
);
669 struct superswitch
*superlist
[] = { &super0
, &super1
, NULL
};
671 struct supertype
*super_by_version(int vers
, int minor
)
673 struct supertype
*st
= malloc(sizeof(*st
));
677 st
->max_devs
= MD_SB_DISKS
;
684 st
->minor_version
= minor
;
688 struct supertype
*guess_super(int fd
)
690 /* try each load_super to find the best match,
691 * and return the best superswitch
693 struct superswitch
*ss
;
694 struct supertype
*st
;
695 unsigned long besttime
= 0;
701 st
= malloc(sizeof(*st
));
702 memset(st
, 0, sizeof(*st
));
703 for (i
=0 ; superlist
[i
]; i
++) {
707 rv
= ss
->load_super(st
, fd
, &sbp
, NULL
);
710 ss
->getinfo_super(&info
, sbp
);
711 if (bestsuper
== -1 ||
712 besttime
< info
.array
.ctime
) {
714 besttime
= info
.array
.ctime
;
719 if (bestsuper
!= -1) {
722 rv
= superlist
[bestsuper
]->load_super(st
, fd
, &sbp
, NULL
);
734 /* tinyc doesn't optimize this check in ioctl.h out ... */
735 unsigned int __invalid_size_argument_for_IOC
= 0;