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 * Parse a 128 bit uuid in 4 integers
37 * format is 32 hexx nibbles with options :.<space> separator
38 * If not exactly 32 hex digits are found, return 0
41 int parse_uuid(char *str
, int uuid
[4])
43 int hit
= 0; /* number of Hex digIT */
46 for (i
=0; i
<4; i
++) uuid
[i
]=0;
52 else if (c
>='a' && c
<= 'f')
54 else if (c
>='A' && c
<= 'F')
56 else if (strchr(":. -", c
))
74 * Get the md version number.
75 * We use the RAID_VERSION ioctl if it is supported
76 * If not, but we have a block device with major '9', we assume
79 * Return version number as 24 but number - assume version parts
83 int md_get_version(int fd
)
88 if (fstat(fd
, &stb
)<0)
90 if ((S_IFMT
&stb
.st_mode
) != S_IFBLK
)
93 if (ioctl(fd
, RAID_VERSION
, &vers
) == 0)
94 return (vers
.major
*10000) + (vers
.minor
*100) + vers
.patchlevel
;
97 if (major(stb
.st_rdev
) == MD_MAJOR
)
103 int get_linux_version()
112 a
= strtoul(cp
, &cp
, 10);
113 if (*cp
!= '.') return -1;
114 b
= strtoul(cp
+1, &cp
, 10);
115 if (*cp
!= '.') return -1;
116 c
= strtoul(cp
+1, NULL
, 10);
118 return (a
*1000000)+(b
*1000)+c
;
121 int enough(int level
, int raid_disks
, int layout
,
122 char *avail
, int avail_disks
)
127 /* This is the tricky one - we need to check
128 * which actual disks are present.
130 copies
= (layout
&255)* (layout
>>8);
133 /* there must be one of the 'copies' form 'first' */
139 first
= (first
+1) % raid_disks
;
144 } while (first
!= 0);
148 return avail_disks
>= 1;
151 return avail_disks
== raid_disks
;
153 return avail_disks
>= 1;
156 return avail_disks
>= raid_disks
-1;
158 return avail_disks
>= raid_disks
-2;
164 int same_uuid(int a
[4], int b
[4], int swapuuid
)
167 /* parse uuids are hostendian.
168 * uuid's from some superblocks are big-ending
169 * if there is a difference, we need to swap..
171 unsigned char *ac
= (unsigned char *)a
;
172 unsigned char *bc
= (unsigned char *)b
;
174 for (i
=0; i
<16; i
+= 4) {
175 if (ac
[i
+0] != bc
[i
+3] ||
176 ac
[i
+1] != bc
[i
+2] ||
177 ac
[i
+2] != bc
[i
+1] ||
192 int check_ext2(int fd
, char *name
)
195 * Check for an ext2fs file system.
196 * Superblock is always 1K at 1K offset
198 * s_magic is le16 at 56 == 0xEF53
199 * report mtime - le32 at 44
201 * logblksize - le32 at 24
203 unsigned char sb
[1024];
206 if (lseek(fd
, 1024,0)!= 1024)
208 if (read(fd
, sb
, 1024)!= 1024)
210 if (sb
[56] != 0x53 || sb
[57] != 0xef)
213 mtime
= sb
[44]|(sb
[45]|(sb
[46]|sb
[47]<<8)<<8)<<8;
214 bsize
= sb
[24]|(sb
[25]|(sb
[26]|sb
[27]<<8)<<8)<<8;
215 size
= sb
[4]|(sb
[5]|(sb
[6]|sb
[7]<<8)<<8)<<8;
216 fprintf(stderr
, Name
": %s appears to contain an ext2fs file system\n",
218 fprintf(stderr
," size=%dK mtime=%s",
219 size
*(1<<bsize
), ctime(&mtime
));
223 int check_reiser(int fd
, char *name
)
226 * superblock is at 64K
228 * Magic string "ReIsErFs" or "ReIsEr2Fs" at 52
231 unsigned char sb
[1024];
233 if (lseek(fd
, 64*1024, 0) != 64*1024)
235 if (read(fd
, sb
, 1024) != 1024)
237 if (strncmp((char*)sb
+52, "ReIsErFs",8)!=0 &&
238 strncmp((char*)sb
+52, "ReIsEr2Fs",9)!=0)
240 fprintf(stderr
, Name
": %s appears to contain a reiserfs file system\n",name
);
241 size
= sb
[0]|(sb
[1]|(sb
[2]|sb
[3]<<8)<<8)<<8;
242 fprintf(stderr
, " size = %luK\n", size
*4);
247 int check_raid(int fd
, char *name
)
253 struct supertype
*st
= guess_super(fd
);
256 st
->ss
->load_super(st
, fd
, &super
, name
);
257 /* Looks like a raid array .. */
258 fprintf(stderr
, Name
": %s appears to be part of a raid array:\n",
260 st
->ss
->getinfo_super(&info
, super
);
262 crtime
= info
.array
.ctime
;
263 level
= map_num(pers
, info
.array
.level
);
264 if (!level
) level
= "-unknown-";
265 fprintf(stderr
, " level=%s devices=%d ctime=%s",
266 level
, info
.array
.raid_disks
, ctime(&crtime
));
274 for (i
=0; i
<5; i
++) {
276 fprintf(stderr
, "%s%s", mesg
, add
);
278 if (fgets(buf
, 100, stdin
)==NULL
)
280 if (buf
[0]=='y' || buf
[0]=='Y')
282 if (buf
[0]=='n' || buf
[0]=='N')
286 fprintf(stderr
, Name
": assuming 'no'\n");
290 char *map_num(mapping_t
*map
, int num
)
300 int map_name(mapping_t
*map
, char *name
)
303 if (strcmp(map
->name
, name
)==0)
311 int is_standard(char *dev
, int *nump
)
313 /* tests if dev is a "standard" md dev name.
314 * i.e if the last component is "/dNN" or "/mdNN",
315 * where NN is a string of digits
317 char *d
= strrchr(dev
, '/');
322 if (strncmp(d
, "/d",2)==0)
323 d
+= 2, type
=1; /* /dev/md/dN{pM} */
324 else if (strncmp(d
, "/md_d", 5)==0)
325 d
+= 5, type
=1; /* /dev/md_dNpM */
326 else if (strncmp(d
, "/md", 3)==0)
327 d
+= 3, type
=-1; /* /dev/mdN */
328 else if (d
-dev
> 3 && strncmp(d
-2, "md/", 3)==0)
329 d
+= 1, type
=-1; /* /dev/md/N */
339 if (nump
) *nump
= num
;
346 * convert a major/minor pair for a block device into a name in /dev, if possible.
347 * On the first call, walk /dev collecting name.
348 * Put them in a simple linked listfor now.
355 int devlist_ready
= 0;
358 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
362 char *map_dev(int major
, int minor
, int create
)
365 fprintf(stderr
, "Warning - fail to map %d,%d to a device name\n",
373 int add_dev_1(const char *name
, const struct stat
*stb
, int flag
)
375 return add_dev(name
, stb
, flag
, NULL
);
377 int nftw(const char *path
, int (*han
)(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
), int nopenfd
, int flags
)
379 return ftw(path
, add_dev_1
, nopenfd
);
383 int add_dev(const char *name
, const struct stat
*stb
, int flag
, struct FTW
*s
)
386 if (S_ISLNK(stb
->st_mode
)) {
391 if ((stb
->st_mode
&S_IFMT
)== S_IFBLK
) {
392 char *n
= strdup(name
);
393 struct devmap
*dm
= malloc(sizeof(*dm
));
394 if (strncmp(n
, "/dev/./", 7)==0)
397 dm
->major
= major(stb
->st_rdev
);
398 dm
->minor
= minor(stb
->st_rdev
);
408 * Find a block device with the right major/minor number.
409 * If we find multiple names, choose the shortest.
410 * If we find a non-standard name, it is probably there
411 * deliberately so prefer it over a standard name.
412 * This applies only to names for MD devices.
414 char *map_dev(int major
, int minor
, int create
)
417 char *std
= NULL
, *nonstd
=NULL
;
420 if (!devlist_ready
) {
423 if (lstat(dev
, &stb
)==0 &&
424 S_ISLNK(stb
.st_mode
))
426 nftw(dev
, add_dev
, 10, FTW_PHYS
);
431 for (p
=devlist
; p
; p
=p
->next
)
432 if (p
->major
== major
&&
434 if (is_standard(p
->name
, NULL
)) {
436 strlen(p
->name
) < strlen(std
))
439 if (nonstd
== NULL
||
440 strlen(p
->name
) < strlen(nonstd
))
444 if (!std
&& !nonstd
&& !did_check
) {
448 if (create
&& !std
&& !nonstd
) {
450 snprintf(buf
, sizeof(buf
), "%d:%d", major
, minor
);
454 return nonstd
? nonstd
: std
;
459 unsigned long calc_csum(void *super
, int bytes
)
461 unsigned long long newcsum
= 0;
464 unsigned int *superc
= (unsigned int*) super
;
466 for(i
=0; i
<bytes
/4; i
++)
468 csum
= (newcsum
& 0xffffffff) + (newcsum
>>32);
470 /* The in-kernel checksum calculation is always 16bit on
471 * the alpha, though it is 32 bit on i386...
472 * I wonder what it is elsewhere... (it uses and API in
473 * a way that it shouldn't).
475 csum
= (csum
& 0xffff) + (csum
>> 16);
476 csum
= (csum
& 0xffff) + (csum
>> 16);
481 char *human_size(long long bytes
)
485 /* We convert bytes to either centi-M{ega,ibi}bytes or
486 * centi-G{igi,ibi}bytes, with appropriate rounding,
487 * and then print 1/100th of those as a decimal.
488 * We allow upto 2048Megabytes before converting to
489 * gigabytes, as that shows more precision and isn't
490 * too large a number.
491 * Terrabytes are not yet handled.
494 if (bytes
< 5000*1024)
496 else if (bytes
< 2*1024LL*1024LL*1024LL) {
497 long cMiB
= (bytes
/ ( (1LL<<20) / 200LL ) +1) /2;
498 long cMB
= (bytes
/ ( 1000000LL / 200LL ) +1) /2;
499 snprintf(buf
, sizeof(buf
), " (%ld.%02ld MiB %ld.%02ld MB)",
500 cMiB
/100 , cMiB
% 100,
503 long cGiB
= (bytes
/ ( (1LL<<30) / 200LL ) +1) /2;
504 long cGB
= (bytes
/ (1000000000LL/200LL ) +1) /2;
505 snprintf(buf
, sizeof(buf
), " (%ld.%02ld GiB %ld.%02ld GB)",
506 cGiB
/100 , cGiB
% 100,
512 char *human_size_brief(long long bytes
)
517 if (bytes
< 5000*1024)
518 snprintf(buf
, sizeof(buf
), "%ld.%02ldKiB",
519 (long)(bytes
>>10), (long)(((bytes
&1023)*100+512)/1024)
521 else if (bytes
< 2*1024LL*1024LL*1024LL)
522 snprintf(buf
, sizeof(buf
), "%ld.%02ldMiB",
524 (long)((bytes
&0xfffff)+0x100000/200)/(0x100000/100)
527 snprintf(buf
, sizeof(buf
), "%ld.%02ldGiB",
529 (long)(((bytes
>>10)&0xfffff)+0x100000/200)/(0x100000/100)
534 int get_mdp_major(void)
536 static int mdp_major
= -1;
540 int have_devices
= 0;
545 fl
= fopen("/proc/devices", "r");
548 while ((w
= conf_word(fl
, 1))) {
549 if (have_block
&& strcmp(w
, "devices:")==0)
551 have_block
= (strcmp(w
, "Block")==0);
554 if (have_devices
&& strcmp(w
, "mdp")==0)
555 mdp_major
= last_num
;
564 char *get_md_name(int dev
)
566 /* find /dev/md%d or /dev/md/%d or make a device /dev/.tmp.md%d */
567 /* if dev < 0, want /dev/md/d%d or find mdp in /proc/devices ... */
568 static char devname
[50];
574 int mdp
= get_mdp_major();
575 if (mdp
< 0) return NULL
;
576 rdev
= makedev(mdp
, (-1-dev
)<<6);
577 snprintf(devname
, sizeof(devname
), "/dev/md/d%d", -1-dev
);
578 if (stat(devname
, &stb
) == 0
579 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
580 && (stb
.st_rdev
== rdev
))
583 rdev
= makedev(MD_MAJOR
, dev
);
584 snprintf(devname
, sizeof(devname
), "/dev/md%d", dev
);
585 if (stat(devname
, &stb
) == 0
586 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
587 && (stb
.st_rdev
== rdev
))
590 snprintf(devname
, sizeof(devname
), "/dev/md/%d", dev
);
591 if (stat(devname
, &stb
) == 0
592 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
593 && (stb
.st_rdev
== rdev
))
596 dn
= map_dev(major(rdev
), minor(rdev
), 0);
599 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md%d", dev
);
600 if (mknod(devname
, S_IFBLK
| 0600, rdev
) == -1)
604 if (stat(devname
, &stb
) == 0
605 && (S_IFMT
&stb
.st_mode
) == S_IFBLK
606 && (stb
.st_rdev
== rdev
))
612 void put_md_name(char *name
)
614 if (strncmp(name
, "/dev/.tmp.md", 12)==0)
618 int dev_open(char *dev
, int flags
)
620 /* like 'open', but if 'dev' matches %d:%d, create a temp
621 * block device and open that
626 int major
= strtoul(dev
, &e
, 0);
628 if (e
> dev
&& *e
== ':' && e
[1] &&
629 (minor
= strtoul(e
+1, &e
, 0)) >= 0 &&
631 snprintf(devname
, sizeof(devname
), "/dev/.tmp.md.%d:%d", major
, minor
);
632 if (mknod(devname
, S_IFBLK
|0600, makedev(major
, minor
))==0) {
633 fd
= open(devname
, flags
);
637 fd
= open(dev
, flags
);
641 struct superswitch
*superlist
[] = { &super0
, &super1
, NULL
};
643 struct supertype
*super_by_version(int vers
, int minor
)
645 struct supertype
*st
= malloc(sizeof(*st
));
649 st
->max_devs
= MD_SB_DISKS
;
656 st
->minor_version
= minor
;
660 struct supertype
*guess_super(int fd
)
662 /* try each load_super to find the best match,
663 * and return the best superswitch
665 struct superswitch
*ss
;
666 struct supertype
*st
;
667 unsigned long besttime
= 0;
673 st
= malloc(sizeof(*st
));
674 memset(st
, 0, sizeof(*st
));
675 for (i
=0 ; superlist
[i
]; i
++) {
679 rv
= ss
->load_super(st
, fd
, &sbp
, NULL
);
682 ss
->getinfo_super(&info
, sbp
);
683 if (bestsuper
== -1 ||
684 besttime
< info
.array
.ctime
) {
686 besttime
= info
.array
.ctime
;
691 if (bestsuper
!= -1) {
694 rv
= superlist
[bestsuper
]->load_super(st
, fd
, &sbp
, NULL
);
706 /* tinyc doesn't optimize this check in ioctl.h out ... */
707 unsigned int __invalid_size_argument_for_IOC
= 0;